- Home /
Sprite animation dependant on object direction
I am making a 2d sprite in a 3d world and have been looking for/thinking of a way to get my 2d sprite (which has a set of images for each of 4 directions) to change which set of images it is using dependent on its current direction that it is either facing/moving (based on the y axis). This is for a click to move by the way, kind of like Ragnarok online.
my thoughts were to find the direction the object is facing and get it to change the animation with if statements.
i.e.
if (playerMove == true){
if (Lookat.y == >0 && <90) {
playFront() ;
}
if (Lookat.y == >179 && <270) {
playLeft() ;
}
if (Lookat.y == >179 && <270) {
playBack() ;
}
if (Lookat.y == >269 && <1) {
playright() ;
}
}
at the current point in time i'm not even sure if i'm using the right approach for this sort of problem.
any input is greatly appreciated.
Answer by Derptastic · Apr 02, 2017 at 09:57 AM
I know the thread is old, but I stumbled on it while searching for a solution myself and eventually pieced one together. I hope it helps someone.
Vector2 me = transform.position \\The object that I want to turn
Vector2 target = *player*.transform.position \\the target I want to turn to (in my case the player)
CalculateAngleForAnim(me, target);
private void CalculateAngleForAnim(Vector2 me, Vector2 target)
{
float angleBetween = AngleBetweenVector2(me, target);
if (angleBetween >= 45 && angleBetween < 135)
{
//up
}
else if (angleBetween >= 135 || angleBetween < -135)
{
//left
}
else if (angleBetween >= -135 && angleBetween < -45)
{
//down
}
else if (angleBetween >= -45 && angleBetween < 45)
{
//right
}
}
private float AngleBetweenVector2(Vector2 vec1, Vector2 vec2)
{
Vector2 diference = vec2 - vec1;
float sign = (vec2.y < vec1.y) ? -1.0f : 1.0f;
return Vector2.Angle(Vector2.right, diference) * sign;
}
http://answers.unity3d.com/questions/317648/angle-between-two-vectors.html
This answer was invaluable to get it working.
Cheers @fermmmm for the comment.
This was extremely useful for me. Thank you very much!
Answer by bdickason · Aug 10, 2014 at 03:41 PM
My suggestion would be to attach an animator object and pass in your facing_x and facing_y variables as floats (or ints) to the animator.
You can then create a blend tree in your animator that says 'if facing_x = 0 and facing_y = 1' load the facing_right sprite.
Here's a great forum thread detailing this approach (look at keely's post): http://forum.unity3d.com/threads/animator-state-machine-for-top-down-2d-games-with-non-rotational-sprite-animation.212172/
I also have a code sample in my game that you can see here: https://github.com/bdickason/stormsword/blob/e03e2fc0b896441d880835533e55d28f7f4ac80e/Assets/Resources/scripts/characters/MoveScript.cs
Your answer
Follow this Question
Related Questions
Sprite Animation - Oneshot 0 Answers
2D Sprite animation: Mesh swap or UV swap? 1 Answer
How do I partially repaint sprite? 0 Answers
2D environment manipulation 0 Answers
Adding texture to cube 1 Answer