Hello Guest

Author Topic: Rotating sprites  (Read 6602 times)

Pulluxx

  • Newbie
  • *
  • Posts: 9
    • View Profile
Rotating sprites
« on: May 30, 2013, 12:20:54 pm »
Hey! I'm working on putting in movement patterns in our game, and we want the animated sprites to look at the position it's going to while moving. This is my code so far (where m_NextNode.m_Position is the Vector3 the animated sprite is heading towards):

Code: [Select]
Quaternion rotation = Quaternion.LookRotation((m_NextNode.m_Position - transform.position).normalized, transform.up);
Vector3 tempRot = rotation.eulerAngles;
tempRot.y -= 90f;
tempRot.z *= -1f;
transform.rotation = Quaternion.Euler(tempRot);

This works pretty OK at certain angles, though at some angles the sprite turns upside down or gets another strange rotation (this happens mostly between vectors with big differences on the y-axis). I'm wondering if you could help me out with the rotation. Is there any rotation function in 2D Toolkit you should use? Do I need to flip the animations in some way (however I haven't had any problem with animated sprites changing 'direction' on the x-axis so far)?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Rotating sprites
« Reply #1 on: May 30, 2013, 12:40:45 pm »
It doesn't need to be that complicated at all. In fact its really really simple.
Code: [Select]
Vector3 direction = targetPoint - transform.position;
transform.eulerAngles = new Vector3(0, 0, Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg);
This makes the +x axis point towards the target. If you want +y axis to follow instead, just subtract 90 from the result :)

Pulluxx

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Rotating sprites
« Reply #2 on: May 30, 2013, 03:15:07 pm »
Unikron, I love you. It works almost perfectly. There's still some problem with the sprite moving backwards on the x-axis though. I decided to split up the calculation like this:

Code: [Select]
if (targetPoint.x > transform.position.x)
{

        Vector3 direction = targetPoint - transform.position;
transform.eulerAngles = new Vector3(0, 0, Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg);
}

On this one everything is great! :)

Code: [Select]
else if (targetPoint.x < transform.position.x)
{

        Vector3 direction = transform.position - targetPoint;
transform.eulerAngles = new Vector3(0, 0, Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg);
}

However, on this one the sprite are moving backwards (moonwalking). I know I could create another atlas with the same animations playing in the opposite direction, but that's kind of a hassle. Is there any other way to do it with the rotation transformation?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Rotating sprites
« Reply #3 on: May 30, 2013, 04:24:43 pm »
flip the sprite?
eg.
sprite.scale = new Vector3(sprite.scale.x * -1, sprite.scale.y, sprite.scale.z);

Pulluxx

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Rotating sprites
« Reply #4 on: May 30, 2013, 04:38:46 pm »
I should probably start looking for the simplest solutions first  :-[

Thanks a lot, you've been of great help!