2D Toolkit Forum

2D Toolkit => Support => Topic started by: wijesijp on August 15, 2013, 09:25:05 am

Title: How to play correct animation based on direction?
Post by: wijesijp 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
Title: Re: How to play correct animation based on direction?
Post by: unikronsoftware 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.