Hello Guest

Author Topic: Best way to rotate a sprite towards a point?  (Read 3516 times)

tellemstevedave

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 4
    • View Profile
Best way to rotate a sprite towards a point?
« 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?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Best way to rotate a sprite towards a point?
« Reply #1 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);