Different animation in 2 directions

240 Views Asked by At

I’m really just a beginner & I have no idea how to use the animation in Unity properly. I’m working on a 2D platformer game. I managed to make the animation work, but the point is that my character should looks different from each side (so I can’t just flip the sprite). What’s the simplest way to for example, when pressing right he will use animation 1 and when pressing left he will use animation 2? Thanks in advance & sorry about my stupidity;)

1

There are 1 best solutions below

0
Owais Shaikh On

Did you try using Animator Controller.

[RequireComponent(typeof(Animator))]
public class PlayerAnimation: MonoBehaviour
{
    [SerializeField]private Animator _animator;
    private const string DirectionParam = "Direction";
    private void Update()
    {
        //On Right button clicked
        if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            //Play Right animation
            _animator.SetInteger(DirectionParam, 1);
        }else if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            _animator.SetInteger(DirectionParam, -1);
        }
        else
        {
            _animator.SetInteger(DirectionParam, 0);
        }
    }
}
  1. Drag this script on you player. This will also Add unity Animator Component on your game object.
  2. Create Animator Object and place it on the animator component.
  3. open Animator Editor from Window>Animation>Animator
  4. Then create an int parameter in Animation Controller Window and name it "Direction".
  5. Create two animation clips one for left move animation and another for right move animation
  6. Drag these clips to the animator Window. Right click and create two transition and add condition with Direction parameter. You should create 3 animation clips and set that to idle and transition to idle when the direction value is 0.

Enjoy!