2D Toolkit Forum
2D Toolkit => Support => Topic started by: GameDragon on November 09, 2013, 02:48:54 am
-
I've searched and used this code to rotate my object to a target:
Vector3 direction = targetPoint - transform.position;
transform.eulerAngles = new Vector3(0, 0, Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg);
But I'd like to add some rotation speed to this to make it rotate more slowly. How would I go about doing this?
-
Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg gives you the angle - what you could to is start from the current transform.eulerAngles.z and then slowly move towards this instead of setting it directly.
-
I've tried:
Vector3 direction = player.transform.position - transform.position;
direction = new Vector3(0, 0, Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg);
transform.eulerAngles = Vector3.Lerp(transform.eulerAngles, direction, speed * Time.deltaTime);
But all that does it make it spin out uncontrollably.
-
Use Math.MoveTowardsAngle to move move the rotation value from the previous value to current. You don't need to lerp all of eulerAngles, just use MoveTowardsAngle on the z value. Check the unity docs for more details.
-
That makes so much more sense. Thank you very much.