Search Unity

Question Animator transition works fine for horizontal but not vertical

Discussion in 'Animation' started by Likutar, Oct 3, 2021.

  1. Likutar

    Likutar

    Joined:
    Aug 15, 2021
    Posts:
    9
    Hi there. I have been trying for hours to figure out why the animation only plays for the horizontal condition but not the vertical conditions. i.g. The animation would only play when the right arrow key is involved.
    My animator transition conditions are...
    1. Horizontal greater than 0.
    2. Vertical greater than 0.
    3. Vertical less than 0.
    ssss.png


    Here is my script:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PlayerSimpleMovements : MonoBehaviour
    {
    public Animator animator;
    private float moveSpeed = 1;
    }

    void FixedUpdate()
    {
    Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0.0f);

    animator.SetFloat("Horizontal", movement.x);
    animator.SetFloat("Vertical", movement.y);

    transform.position = transform.position + movement * moveSpeed * Time.deltaTime;
    }

    }
     
  2. Likutar

    Likutar

    Joined:
    Aug 15, 2021
    Posts:
    9
    Is there anyone that could help?
     
  3. Masea1

    Masea1

    Joined:
    Jun 9, 2018
    Posts:
    41
    First of all, I see one unnecessary bracket in your code after your declared your variables. Is the code even working?

    Moreover, I'd suggest you use Update() method instead of FixedUpdate() because that's for physics-related stuff and as far as the code you posted is concerned, I can't see any physics-related stuff there.
     
  4. Likutar

    Likutar

    Joined:
    Aug 15, 2021
    Posts:
    9
    Thanks for the reply. Maybe it's an error when I copied and pasted. Everything goes smooth with the script and there's only one thing I still cannot figure out. The animation works fine when the character walks anywhere when x is not 0, but when the character walks directly upwards or downwards (vertical) the transition seems not being activated.

    I have set up 3 separated transitions (3 arrows) to trigger the animation from "IdleRight" to "WalkRight".
    1. Horizontal greater than 0.
    2. Vertical greater than 0.
    3. Vertical less than 0.
    But the result was only number 1 that triggers the transition fine.

    Can anyone explain?

    p.s. The reason why I used FixedUpdate() instead cuz it fixes the vibration issue when the character walks into a box at the scene.