播放器的旋转略有偏离

时间:2018-09-10 21:26:49

标签: c# rotation transform

我是unity / c#的新手,在播放器旋转方面需要一些帮助。当我将鼠标光标停留在y轴的正方向(如果播放器的中间为0)时,播放器的视线将略微位于光标上方,而当鼠标轴的y负轴时,播放器的视线将略位于光标下方。这是演示的gif:
https://gyazo.com/e417962c20e186f3c6419c23bf8263f6

这是我的轮换代码。

public class LookTowardMouse : MonoBehaviour 
{
    void Update()
    {
        //Get the Screen positions of the object
        Vector2 positionOnScreen = Camera.main.WorldToViewportPoint(transform.position);

        //Get the Screen position of the mouse
        Vector2 mouseOnScreen = (Vector2)Camera.main.ScreenToViewportPoint(Input.mousePosition);

        //Get the angle between the points
        float angle = AngleBetweenTwoPoints(positionOnScreen, mouseOnScreen);

        //Rotate player

        Debug.Log(angle);
        transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, angle));
    }

    float AngleBetweenTwoPoints(Vector3 a, Vector3 b)
    {
        return Mathf.Atan2(a.y - b.y, a.x - b.x) * Mathf.Rad2Deg;
    } 
}

0 个答案:

没有答案