首页 > 解决方案 > 2d 射击鼠标跟随问题。统一二维

问题描述

我正在制作 2d 射击游戏,但遇到了问题。当我的角色在向左移动时将其比例翻转为 -1 时,武器握把的旋转会从光标处反转。这是我有的鼠标跟随代码,以备不时之需。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;



    public float offset;



    void Update()
    {
        Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
        float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.Euler(0f, 0f, rotZ + offset);


    }

任何帮助都是使用完整的。

标签: c#unity3d

解决方案


用于Mathf.Sign在计算旋转时考虑 x 刻度的符号。当刻度翻转时,您将使用它来否定旋转的 x 分量和旋转角度:

Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) 
        - transform.position;
float scaleSign = Mathf.Sign(transform.localScale.x);
float rotZ = Mathf.Atan2(difference.y, difference.x * scaleSign) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, (rotZ + offset) * scaleSign );

推荐阅读