Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

2D Animation glitches

Discussion in '2D' started by Aron95, Jul 31, 2021.

  1. Aron95

    Aron95

    Joined:
    Jun 22, 2020
    Posts:
    3
    Hello Guys,

    at the moment im working on a charakter controller for a game i want to build. Here is the code at the moment for my movement and animation. The input just works fine and the animations are switching. But i have a problem when im switching the input form x to y like pressing "W" for walking up and change it to "D" the animation will switch a current time between the animation for "W" and "D". Here is is a Video from my problem on Youtube. Its my actual project.



    Here is the Code for the Input Controll:

    Code (CSharp):
    1.  void Update()
    2.     {
    3.      
    4.             xAxis = Input.GetAxisRaw("Horizontal");
    5.             yAxis = Input.GetAxisRaw("Vertical");
    6.  
    7.             Debug.Log("y = "+yAxis);
    8.             Debug.Log("x = "+xAxis);
    9.             animator.SetFloat("Horizontal", Input.GetAxis("Horizontal"));
    10.             animator.SetFloat("Vertical", Input.GetAxis("Vertical"));
    11.      
    12.                
    13.     }
    14.  
    15.     private void FixedUpdate()
    16.     {
    17.  
    18.         attackDelay -= Time.deltaTime;
    19.         if (attackDelay <0)
    20.         {
    21.            
    22.             if (Input.GetButton("Fire1"))
    23.             {
    24.                 Debug.Log("Fire1 Pressed");
    25.                 animator.SetBool("isAttacking", true);
    26.                 attackDelay = 1.5f;
    27.                 StartCoroutine("waiter");
    28.             }
    29.         }
    30.      
    31.      
    32.         gameObject.transform.position = new Vector2(transform.position.x +
    33.             (xAxis * speed), transform.position.y + (yAxis * speed));
    34.     }

    Would be nice if somebody can help me.

    I wish you a nice day.

    Greetings Peter
     
  2. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    906
    Hello Peter,
    In an animation controller, only one state can be active at one time (apart from when switching between two states). In your controller, you have all movement directions linked with the Any State node, and the switch condition is based on horizontal and vertical "speed". If you then press W (increase vertical speed) and D (increase horizontal speed) both the condition for the Up animation state and the Right animation state are true and the animation controller tries to switch between them.

    A solution to this is having a look at 2D blend trees. 2D blend trees are nodes which can contain multiple animation clips and switches between them based on two parameters (usually horizontal and vertical speed).

    Best of luck, and let us know how it goes!
     
  3. Aron95

    Aron95

    Joined:
    Jun 22, 2020
    Posts:
    3
    Hello Ted,

    Thanks alot i will dig deeper into 2d blend trees.
     
    Ted_Wikman likes this.