- Home /
Rotation of Player isn't very accurate
I have been attempting to make it so the player rotates and looks based off of where the mouse is. I have almost gotten it to work, however, when the mouse isn't at one of the 90 degree angles around the player, it is off, most of all when you hit directly in between the 90 degrees, at the 45 degree marks. I don't understand why it would be off, though I wonder if it has something to do with the fact that C#'s Mathf.Atan2 returns a radian measure and I changed it to degrees.
private Vector2 mousepos;
private Vector2 playerpos;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
mousepos = (Vector2)Camera.main.ScreenToViewportPoint(Input.mousePosition);
playerpos = Camera.main.WorldToViewportPoint(transform.position);
float angle = Mathf.Atan2(playerpos.y - mousepos.y, playerpos.x - mousepos.x) * Mathf.Rad2Deg;
angle += 90;
transform.rotation = Quaternion.Euler(0.0f, 0.0f, angle);
}
Any help would be appreciated, as I am still figuring out how angles in C# works.
,
Your answer
Follow this Question
Related Questions
Counting a 2D objects flips, full 360 rotation on the Z axis. C# 0 Answers
How do I get the sprite pointing the right direction? 1 Answer
How do I rotate a 2D object based on it's velocity? 1 Answer
Sword rotation not working? 1 Answer
Rotation approximation issues using Quaternion.LookRotation 0 Answers