Unity Animation Dont work

Hey! i have been making a project and im stuck on this.

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

public class CharacterAnimationController : MonoBehaviour
{
    public bool walk, jump, run;
    public Movement controller;
    public Animator anim;
    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
      

        if (controller.run && controller.direction.magnitude >= 0.1f)
        {
            run = true;
        }
        else if(!controller.run && controller.direction.magnitude < 0.1f)
        {
            run = false;
        }
        if (controller.walk)
        {
            walk = true;
        }
        else
        {
            walk = false;
        }

        if (walk)
        {
            Debug.Log("walk");
            anim.SetBool("Idle", false);
            anim.SetBool("Walk", true);
           [B] [/B]anim.SetBool("Walk", true); //this part dont work
        }
        else
        {
            anim.SetBool("Idle", true);
            anim.SetBool("Sprint", false);
            anim.SetBool("Walk", false);
        }
      
        if (run)
        {
            anim.SetBool("Idle", false);
            anim.SetBool("Sprint", true);
            anim.SetBool("Walk", false);
        }
        else
        {
            anim.SetBool("Idle", true);
            anim.SetBool("Sprint", false);
            anim.SetBool("Walk", false);
        }

       
       
    }

 
}

When im starting the project, if i move animation isn’t enabled (bool)

if (walk)
        {
            Debug.Log("walk");
            anim.SetBool("Idle", false);
            anim.SetBool("Walk", true);
           [B] [/B]anim.SetBool("Walk", true); //this part dont work
        }
        else
        {
            anim.SetBool("Idle", true);
            anim.SetBool("Sprint", false);
            anim.SetBool("Walk", false);
        }
  
        if (run)
        {
            anim.SetBool("Idle", false);
            anim.SetBool("Sprint", true);
            anim.SetBool("Walk", false);
        }
        else
        {
            anim.SetBool("Idle", true);
            anim.SetBool("Sprint", false);
            anim.SetBool("Walk", false);
        }

You should insert debug.log statements and check your values to make sure they are getting set as you expect. However, this bit of code here is not going to work.

Why? Quite simply anytime walk is true, run should be false. Which means when the first if statement happens, you set the “Walk” to true… however, right after, you toggle “Walk” back to false because the run if check happens, and run would be false.

Always walk through your logic. Basically with these two if checks unless run is true, your character will always resolve to idling.

So, actually, it’s doing exactly what you tell it to. :slight_smile:

1 Like