Search Unity

Player stuck in Idle?!?!?

Discussion in 'Animation' started by SalvaG, Apr 13, 2014.

  1. SalvaG

    SalvaG

    Joined:
    Apr 10, 2014
    Posts:
    13
    So my player is stuck in idle, he won't switch to either Walk or Run. Another problem that I have is when i don't move my players feet are in the ground when I make an movement his feet are on the floor. But the main problem is that he won't switch between animation. I have is setup that if I press W = Walk, Shift+W = Run the keys adjust the speed but don't start a new animation.

    My PlayerMovement Script:

    using UnityEngine;
    using System.Collections;

    public class PlayerMovement : MonoBehaviour {

    float speed = 8.0f;

    Vector3 direction = Vector3.zero;

    CharacterController cc;

    // Use this for initialization
    void Start () {
    cc = GetComponent<CharacterController> ();
    }

    // Update is called once per frame
    void Update () {
    direction = transform.rotation * new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical")).normalized;
    if (Input.GetKey (KeyCode.LeftShift)) {
    speed = 8.0f;

    } else if (!Input.GetKey (KeyCode.LeftShift)) {
    speed = 4.0f;
    }
    }
    void FixedUpdate (){
    cc.SimpleMove(direction * speed);
    }
    }



    I don't get any errors just it won't switch from Idle to either Walk or Run. I have been stuck with this for about 3 days trying to fix this.
    (I can't add the script as an Attachment in this post I have tried but it won't work I'm sorry for this)
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    9,609
    When posting code, please wrap it in [code ] and [/code ] tags to format it properly.

    In your script, you're moving the CharacterController but you're not doing anything with animation.

    If you're using Mecanim, use GetComponent<Animator>() and set the parameter that you've defined in your Animator Controller (e.g., SetFloat(Speed, x)).

    If you're using Legacy, use animation.CrossFade() to switch to the appropriate animation (idle, walk, or run).
     
  3. SalvaG

    SalvaG

    Joined:
    Apr 10, 2014
    Posts:
    13
    So I addet the GetCompnent<Animator>() already I figured that out. I set the parameters up in the Mecanim system. Now I need to do 1 thing and that is add that to the code but how I tried looking it up but no luck.

    My Code now (I can't add code tags in reply sorry)
    using UnityEngine;
    using System.Collections;

    public class PlayerMovement : MonoBehaviour {

    public class PlayerController : MonoBehaviour
    {

    public class PlayerMovement : MonoBehaviour {

    float speed = 8.0f;

    Vector3 direction = Vector3.zero;
    CharacterController cc;

    // Use this for initialization
    void Start () {
    cc = GetComponent<CharacterController> ();
    GetComponent<Animator> ();
    }

    // Update is called once per frame
    void Update () {
    direction = transform.rotation * new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical")).normalized;
    if (Input.GetKey (KeyCode.LeftShift)) {
    speed = 8.0f;

    } else if (!Input.GetKey (KeyCode.LeftShift)) {
    speed = 4.0f;
    }
    }
    void FixedUpdate (){
    cc.SimpleMove(direction * speed);
    }
    }
    }
    }
     
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    9,609
    (You can just type in the code tags manually, or click the Go Advanced button. In the advanced editor, highlight your code and click the "#" tool.)

    Try something like this:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerMovement : MonoBehaviour {
    6.  
    7.     float speed = 8.0f;
    8.  
    9.     Vector3 direction = Vector3.zero;
    10.     CharacterController cc;
    11.     Animator animator;
    12.  
    13.     // Use this for initialization
    14.     void Start () {
    15.         cc = GetComponent<CharacterController> ();
    16.         animator = GetComponent<Animator> ();
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update () {
    21.         direction = transform.rotation * new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical")).normalized;
    22.         if (Input.GetKey (KeyCode.LeftShift)) {
    23.             speed = 8.0f;
    24.  
    25.         } else if (!Input.GetKey (KeyCode.LeftShift)) {
    26.             speed = 4.0f;
    27.         }
    28.         animator.SetFloat("Speed", speed);
    29.     }
    30.  
    31.     void FixedUpdate (){
    32.         cc.SimpleMove(direction * speed);
    33.     }
    34. }
    35. }
    36.  
    A couple notes:
    • This code assumes that your Animator Controller has a parameter named Speed that actually transitions animations.
    • The way your code is written, speed will always be 4 (walk) or 8 (run), never 0 (idle).
    • Download and study the Locomotion Starter Kit. It's free.
    • Watch the animation tutorials.