Walking Trouble

I’m attempting to get my player to walk, but he keeps sliding across the ground and stuck in his idle phase. When viewing the animator, it shows me that he never moves to the walking animation and the idle just keeps playing. I have set the Idle to Walk when the Speed is greater than 0.1. Then I set the Walk to Idle when Speed is less than 0.1. Do I need to mention the animations in script?

Do you have a script on your game object that change the speed value based on some input?

something like animator.SetFloat(“Speed”, mySpeed);

I did state the speed in the player movement script:

public float speed = 1f;

And set the speed as 0.1 (pictured below on the left under “Play Control (Script)”)


But other than that, a float was not set for the speed. If I set the walking as default in the animator, the player will walk but still won’t transition from walk to idle (let alone show that it’s working).

do you pass the speed var to the animator?
something like:

int speedHash = Animator.StringToHash("Speed");
float speed = 1f;
Animator anim = GetComponent<Animator>();
void Update()
{
anim.SetFloat(speedHash, speed);
}
1 Like

In this case ‘speed’ is a a parameter of the controller, to change from idle to walk the value of speed must change and go above 0.1 and to switch back from walk to idle ‘speed’ must go below 0.1.

Like Berkk wrote, you need to update the value of speed on each update. If it a player avatar it could be a speed computed with controller Input value.

using UnityEngine;

public class MyController : MonoBehaviour {

    Animator animator;
    int speedHash = Animator.StringToHash("Speed");

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

    // Update is called once per frame
    void Update()
    {
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");
        Vector2 speedVec = new Vector2(horizontal, vertical);
        float speed = = Mathf.Clamp(speedVec.magnitude, 0, 1);
        animator.SetFloat(speedHash, speed);

    }
}
1 Like

I’m sorry, I’m such a newbie to this. Is this correct?

using UnityEngine;
using System.Collections;

public class PlayerControl : MonoBehaviour
    {
    public float speed = 1f;
    Animator GuardianWalk;
    int speedHash = Animator.StringToHash("Speed");

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


    void Update () {
        //Move player right
        if (Input.GetKey (KeyCode.D)) {
            transform.position += new Vector3 (speed + Time.deltaTime, 0.0f, 0.0f);
            transform.eulerAngles =new Vector2(0, 0);
        }
        //Move player left
        if (Input.GetKey (KeyCode.A)) {
            transform.position -= new Vector3 (speed + Time.deltaTime, 0.0f, 0.0f);
            transform.eulerAngles = new Vector2(0,180);
        }
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");
        Vector2 speedVec = new Vector2(horizontal, vertical);
        float speed = Mathf.Clamp(speedVec.magnitude, 0, 1);
        animator.SetFloat(speedHash, speed);
    }
}

something like this should work

using UnityEngine;
using System.Collections;
public class PlayerControl : MonoBehaviour
    {
    public float speed;
    Animator GuardianWalk;
    int speedHash = Animator.StringToHash("Speed");
    void Start (){
        ///your Animator component is named GuardianWalk.. not animator.
        GuardianWalk = GetComponent<Animator>();
        }
    void Update () {
        ///just a detail - you should calculate speed variable before you use it to move transform..at your code speed variable is calculated at current frame but used at the nextone (if it makes sense)
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");
        Vector2 speedVec = new Vector2(horizontal, vertical);
        float speed = Mathf.Clamp(speedVec.magnitude, 0, 1);
        //Move player right
        if (Input.GetKey (KeyCode.D)) {
            transform.position += new Vector3 (speed + Time.deltaTime, 0.0f, 0.0f);
            transform.eulerAngles =new Vector2(0, 0);
        }
        //Move player left
        if (Input.GetKey (KeyCode.A)) {
            transform.position -= new Vector3 (speed + Time.deltaTime, 0.0f, 0.0f);
            transform.eulerAngles = new Vector2(0,180);
        }
       ///update the speed variable (pass it to the animator component) - GuardianWalk
        GuardianWalk.SetFloat(speedHash, speed);
    }
}
1 Like

If you want to move transform component to move the object you should do it in OnAnimatorMove callback, otherwise you may get unexpected result. This way the animator will blend correctly any motion for the root.

using System.Collections;
public class PlayerControl : MonoBehaviour
    {
    public float speed;
    Animator GuardianWalk;
    int speedHash = Animator.StringToHash("Speed");
    void Start (){
        ///your Animator component is named GuardianWalk.. not animator.
        GuardianWalk = GetComponent<Animator>();
        }
    void Update () {
        ///just a detail - you should calculate speed variable before you use it to move transform..at your code speed variable is calculated at current frame but used at the nextone (if it makes sense)
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");
        Vector2 speedVec = new Vector2(horizontal, vertical);
        speed = Mathf.Clamp(speedVec.magnitude, 0, 1);
      
       ///update the speed variable (pass it to the animator component) - GuardianWalk
        GuardianWalk.SetFloat(speedHash, speed);
    }

void OnAnimatorMove()
{
        //Move player right
        if (Input.GetKey (KeyCode.D)) {
            transform.position += new Vector3 (speed + Time.deltaTime, 0.0f, 0.0f);
            transform.eulerAngles =new Vector2(0, 0);
        }
        //Move player left
        if (Input.GetKey (KeyCode.A)) {
            transform.position -= new Vector3 (speed + Time.deltaTime, 0.0f, 0.0f);
            transform.eulerAngles = new Vector2(0,180);
        }
}
}
1 Like

Thanks for all your help, guys! :slight_smile: