2D Toolkit Forum

2D Toolkit => Support => Topic started by: tellemstevedave on June 23, 2013, 03:35:15 am

Title: Best way to rotate a sprite towards a point?
Post by: tellemstevedave on June 23, 2013, 03:35:15 am
I could use some help.  I have a ship in my scene that follows a path successfully.  I want the ship to orient itself to the path with a maximum rate of rotation.  I'd really like to use rotateTowards as it seems to handle all of this.  But the problem is that this function and many others assume that and object with a local rotation of 0,0,0 is facing forward on z.

What is the best way for me to rotate a sprite x degress per second towards a point?
Title: Re: Best way to rotate a sprite towards a point?
Post by: unikronsoftware on June 23, 2013, 11:19:49 am
RotateTowards will work fine, it doesn't exactly impose anything - it takes a vector (use transform.up instead of forward as in the sample), and then get it to rotate towards your target. After you get the rotated vector, simply set it like this:
transform.eulerAngles = new Vector3(0, 0, Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg);