Answer the question
In order to leave comments, you need to log in
Unity - how to make a uniform rotation towards the cursor?
I have a script for a 2d game, and this part of the script is responsible for turning the tank's turret:
Quaternion TurretRotation()
{
Vector3 mouse = Input.mousePosition;
mouse.z = Camera.main.transform.position.z;
Vector3 direction = Camera.main.ScreenToWorldPoint(mouse) - transform.position;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
return Quaternion.AngleAxis(angle, Vector3.forward);
}
Answer the question
In order to leave comments, you need to log in
try this code
i recently discovered turns myself, maybe it will help
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
difference.Normalize();
float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, rotationZ);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question