Horizontal movement not working, but vertical movement working...

Hello, this is my first time using animation and I don’t understand why only the up and down animations will play, not the left and right ones.

Here is my code:

public class PlayerMovement : MonoBehaviour
{
    public Rigidbody2D rb2d;

    public float runspeed = 5f;
    public bool moving;

    public float horizontal;
    public float vertical;

    public Animator animator;
    void Start()
    {
        //rigidbody is called and linked to variable
        rb2d = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        //player input is used through the axis, equal to float variables
        horizontal = Input.GetAxisRaw("Horizontal");
        vertical = Input.GetAxisRaw("Vertical");

        animator.SetFloat("Horizontal", horizontal);
        animator.SetFloat("Vertical", vertical);
        animator.SetBool("Moving", moving);

        //if there is input, moving in the animator is set to true and the movement animations are played; if not, idle is played
        {
            if (Input.GetButton("Right"))
            {
                moving = true;
            }
            if (Input.GetButton("Up"))
            {
                moving = true;
            }
            if (Input.GetButton("Left"))
            {
                moving = true;
            }
            if (Input.GetButton("Down"))
            {
                moving = true;
            }
            else
            {
                moving = false;
            }
        }
    }

    private void FixedUpdate()
    {
        //how the speed of the rigidbody should be calculated in both directions
        rb2d.velocity = new Vector2(horizontal * runspeed * Time.deltaTime, vertical * runspeed * Time.deltaTime);
    }
}

I can see in the animator that when I move left and right it is stuck on idle. It won’t change to the movement blend tree I created like when I am moving up and down.

I appreciate any help, thank you :slight_smile:
7695127--963205--Unity forum animation 4.PNG 7695127--963208--Unity forum animation 3.PNG

Your if / else block starting on line 30 is flawed.

Ask yourself what happens if ONLY the Right direction is held: the Down direction will NOT be held, so the moving = false will fire.

Instead, use this approach:

  1. set moving false
  2. check each direction and if the button is pressed, set it true

That will at least fix one bug. If there are more, here is how to interactively debug:

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

Thank you so much, taking what advice you gave me, I used Debug.log, firstly working out that the buttons in the project settings were faulty. After this, I also realised my code was far too complicated.

Below I had a much simpler solution:

        {
            if (rb2d.velocity.magnitude > 0)
            {
                moving = true;
            }
            else
            {
                moving = false;
            }
        }
2 Likes

Simple is ALWAYS better. Glad you’re back in business!

1 Like

This!

It also might help if you consider that your condition can be used to set a bool because after all, the condition produces a bool so this is valid too and much more concise:

moving = rb2d.velocity.magnitude > 0;
1 Like