Hello Guest

Author Topic: How to play correct animation based on direction?  (Read 3001 times)

wijesijp

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 10
    • View Profile
How to play correct animation based on direction?
« on: August 15, 2013, 09:25:05 am »
I am making a tower defense game and I am using 2d toolkit to animate the creeps.  I have 4 animations for left, right, up and down movement. I also have the direction vector of the creep.

How do I decide which animation to play using the direction vector?

For each Update I need to do a test to decide which animation to play.
Even if the creep is move at an angle I should still select one of these animations. 
Looking for a simple method to decide on the correct animation.

If I do this direction test by adding a animation event will that be less costly than doing the direction test in Update() function. My animations have 8 frames
« Last Edit: August 15, 2013, 12:16:05 pm by wijesijp »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to play correct animation based on direction?
« Reply #1 on: August 15, 2013, 01:42:40 pm »
You need to work out the direction your character is facing. You can get the angle with
Mathf.Atan2(transform.up.y, transform.up.x) * Mathf.Rad2Deg - 90 and then choose the correct animation based on the angle range for each animation.