Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Walking Trouble

Discussion in 'Animation' started by TBubernak, Nov 26, 2014.

  1. TBubernak

    TBubernak

    Joined:
    Mar 21, 2014
    Posts:
    15
    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?
     
  2. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Do you have a script on your game object that change the speed value based on some input?

    something like animator.SetFloat("Speed", mySpeed);
     
  3. TBubernak

    TBubernak

    Joined:
    Mar 21, 2014
    Posts:
    15
    I did state the speed in the player movement script:
    Code (CSharp):
    1. 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).
     
  4. Bekkk

    Bekkk

    Joined:
    Jul 28, 2012
    Posts:
    17
    do you pass the speed var to the animator?
    something like:
    Code (CSharp):
    1. int speedHash = Animator.StringToHash("Speed");
    2. float speed = 1f;
    3. Animator anim = GetComponent<Animator>();
    4. void Update()
    5. {
    6. anim.SetFloat(speedHash, speed);
    7. }
     
    theANMATOR2b likes this.
  5. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    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.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class MyController : MonoBehaviour {
    4.  
    5.     Animator animator;
    6.     int speedHash = Animator.StringToHash("Speed");
    7.  
    8.     void Start()
    9.     {
    10.         animator = GetComponent<Animator>();
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.         float horizontal = Input.GetAxis("Horizontal");
    17.         float vertical = Input.GetAxis("Vertical");
    18.         Vector2 speedVec = new Vector2(horizontal, vertical);
    19.         float speed = = Mathf.Clamp(speedVec.magnitude, 0, 1);
    20.         animator.SetFloat(speedHash, speed);
    21.  
    22.     }
    23. }
    24.  
     
    theANMATOR2b likes this.
  6. TBubernak

    TBubernak

    Joined:
    Mar 21, 2014
    Posts:
    15
    I'm sorry, I'm such a newbie to this. Is this correct?
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerControl : MonoBehaviour
    6.     {
    7.     public float speed = 1f;
    8.     Animator GuardianWalk;
    9.     int speedHash = Animator.StringToHash("Speed");
    10.  
    11.     void Start (){
    12.         animator = GetComponent<Animator>();
    13.         }
    14.  
    15.  
    16.     void Update () {
    17.         //Move player right
    18.         if (Input.GetKey (KeyCode.D)) {
    19.             transform.position += new Vector3 (speed + Time.deltaTime, 0.0f, 0.0f);
    20.             transform.eulerAngles =new Vector2(0, 0);
    21.         }
    22.         //Move player left
    23.         if (Input.GetKey (KeyCode.A)) {
    24.             transform.position -= new Vector3 (speed + Time.deltaTime, 0.0f, 0.0f);
    25.             transform.eulerAngles = new Vector2(0,180);
    26.         }
    27.         float horizontal = Input.GetAxis("Horizontal");
    28.         float vertical = Input.GetAxis("Vertical");
    29.         Vector2 speedVec = new Vector2(horizontal, vertical);
    30.         float speed = Mathf.Clamp(speedVec.magnitude, 0, 1);
    31.         animator.SetFloat(speedHash, speed);
    32.     }
    33. }
     
  7. Bekkk

    Bekkk

    Joined:
    Jul 28, 2012
    Posts:
    17
    something like this should work
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. public class PlayerControl : MonoBehaviour
    5.     {
    6.     public float speed;
    7.     Animator GuardianWalk;
    8.     int speedHash = Animator.StringToHash("Speed");
    9.     void Start (){
    10.         ///your Animator component is named GuardianWalk.. not animator.
    11.         GuardianWalk = GetComponent<Animator>();
    12.         }
    13.     void Update () {
    14.         ///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)
    15.         float horizontal = Input.GetAxis("Horizontal");
    16.         float vertical = Input.GetAxis("Vertical");
    17.         Vector2 speedVec = new Vector2(horizontal, vertical);
    18.         float speed = Mathf.Clamp(speedVec.magnitude, 0, 1);
    19.         //Move player right
    20.         if (Input.GetKey (KeyCode.D)) {
    21.             transform.position += new Vector3 (speed + Time.deltaTime, 0.0f, 0.0f);
    22.             transform.eulerAngles =new Vector2(0, 0);
    23.         }
    24.         //Move player left
    25.         if (Input.GetKey (KeyCode.A)) {
    26.             transform.position -= new Vector3 (speed + Time.deltaTime, 0.0f, 0.0f);
    27.             transform.eulerAngles = new Vector2(0,180);
    28.         }
    29.        ///update the speed variable (pass it to the animator component) - GuardianWalk
    30.         GuardianWalk.SetFloat(speedHash, speed);
    31.     }
    32. }
     
    theANMATOR2b likes this.
  8. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    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.

    Code (CSharp):
    1. using System.Collections;
    2. public class PlayerControl : MonoBehaviour
    3.     {
    4.     public float speed;
    5.     Animator GuardianWalk;
    6.     int speedHash = Animator.StringToHash("Speed");
    7.     void Start (){
    8.         ///your Animator component is named GuardianWalk.. not animator.
    9.         GuardianWalk = GetComponent<Animator>();
    10.         }
    11.     void Update () {
    12.         ///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)
    13.         float horizontal = Input.GetAxis("Horizontal");
    14.         float vertical = Input.GetAxis("Vertical");
    15.         Vector2 speedVec = new Vector2(horizontal, vertical);
    16.         speed = Mathf.Clamp(speedVec.magnitude, 0, 1);
    17.      
    18.        ///update the speed variable (pass it to the animator component) - GuardianWalk
    19.         GuardianWalk.SetFloat(speedHash, speed);
    20.     }
    21.  
    22. void OnAnimatorMove()
    23. {
    24.         //Move player right
    25.         if (Input.GetKey (KeyCode.D)) {
    26.             transform.position += new Vector3 (speed + Time.deltaTime, 0.0f, 0.0f);
    27.             transform.eulerAngles =new Vector2(0, 0);
    28.         }
    29.         //Move player left
    30.         if (Input.GetKey (KeyCode.A)) {
    31.             transform.position -= new Vector3 (speed + Time.deltaTime, 0.0f, 0.0f);
    32.             transform.eulerAngles = new Vector2(0,180);
    33.         }
    34. }
    35. }
     
    theANMATOR2b likes this.
  9. TBubernak

    TBubernak

    Joined:
    Mar 21, 2014
    Posts:
    15
    Thanks for all your help, guys! :)