If else if else How to use them | Idle direction based on last walk direction | Multiple booleans in if statement?

Hello, I’m new to coding and this is my first topic. I’m Currently working on character movement/animations (unity 2D). I’m having a issue with the last directions for idle. I created two booleans with ‘directionRight’ ‘directionDown’ and used it for the ‘xAxis’ and ‘yAxis’ but the ‘yAxis’ doesn’t seems to work properly. The ‘xAxis’ works but the ‘yAxis’ only has his walk animations and not his idle animation. My theorie is that I messed up with using so much ‘else if’ statements or I’m not able to use two booleans in one if statement. I hope this is clear if it’s not clear pls correct me. Here is my code (In 'fixedupdate’is where you can find my issue I think):

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

public class PlayerMovement : MonoBehaviour
{
    private Animator animator;

    private string currentState;

    private Rigidbody2D rb;

    float walkSpeed = 4f;
    float xAxis;
    float yAxis;

    public bool directionRight = true;
    public bool directionDown = false;

    const string Walk_U = "Walk_Up";
    const string Walk_D = "Walk_Down";
    const string Walk_L = "Walk_Left";
    const string Walk_R = "Walk_Right";

    const string Idle_U = "Idle_Up";
    const string Idle_D = "Idle_Down";
    const string Idle_L = "Idle_Left";
    const string Idle_R = "Idle_Right";

    void Start()
    {
        animator = GetComponent<Animator>();
        rb = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        xAxis = Input.GetAxisRaw("Horizontal");
        yAxis = Input.GetAxisRaw("Vertical");
    }

    void FixedUpdate()
    {
        rb.velocity = new Vector2(xAxis, yAxis).normalized * walkSpeed;

        if(xAxis > 0f)
        {
            ChangeAnimationState(Walk_R);
            directionRight = true;
        }else if(xAxis < 0f)
        {
            ChangeAnimationState(Walk_L);
            directionRight = false;
        }else if(yAxis > 0f)
        {
            ChangeAnimationState(Walk_U);
            directionDown = false;
        }else if(yAxis < 0f)
        {
            ChangeAnimationState(Walk_D);
            directionDown = true;
        }else if(directionRight)
        {
            ChangeAnimationState(Idle_R);
        }else if(!directionRight)
        {
            ChangeAnimationState(Idle_L);
        }else if(directionDown)
        {
            ChangeAnimationState(Idle_D);
        }else if(!directionDown)
        {
            ChangeAnimationState(Idle_U);
        }
        
    }

    void ChangeAnimationState(string newAnim)
    {
        if(currentState == newAnim) return;
        animator.Play(newAnim);
        currentState = newAnim;
    }
}

[1]:

via+GIPHY

Hello!

My theorie is that I messed up with using so much 'else if” Yes you did :D!

I did not read your code to know exactly what you need, thats your job. I will explain you how to use correctly if, else if and else because i see you dont get exactly how they work. In your code, only 1 statement will be choosed.

At the ond of post, you will see how it works, promise :smiley:

first, with this simple code:

if (x>4) "a";
else if (x>6) "b"

“a” and “b” are possible results. But, if you look, its 100% impossible to get a “b” . Because code is executed line by line, and the else if is linked to first if. So, if actually x=7 (x>4 is true && x>6 is also true) the result will be “a”, never “b”, because the code will never read the else if line. Because the else if will be readed ONLY if all above condition statements are false.

Then, else (not else if, just else) is always the last condition (is optional, but it must be always the lastone if you use it). Else will be executed only if all other condition before are false. This else is alse linked to an if. So the order must be always:

if (always the 1st)
else if (optional)
else if (optional)
... ...
else (optional)

All this we just saw its a conditional block, a complete block. Only ONE thing of all these if, else if and else will be executed. The firstone to be true will be executed, all others will be ignored.

Lets see this example:

if (x>5) "a"
else if (x>2) "b"
else if (x>4) "c"
else "d"

Think in what i just explained, lets see all this values which result will give. (and think why!)

x = ....

1 ---> d
2 ---> d
3 ---> b
4 ---> b
5 ---> c
6 ---> a
7 ---> a
..... always a

BUT, if we use this code, with 2 blocks. the first block if + else if and second block if + else look now at the results:

if (x>5) "a"
else if (x>2) "b"

if (x>4) "c"
else "d"

1 --->  (first block have no result),  second block: d
2 --->  (first block have no result), d
3 ---> b, d       first and second block now give a result
4 ---> b, d
5 ---> b, c
6 ---> a, c
7 ---> a, c
....... always a, c, 

So, each block can give a “true”, only one per block. I expect now that you, with your code and your project think how do you have to organise your if else if and else statements.

Good luck!