我是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;
}
}