Search Unity

Animation states play out of order

Discussion in 'Animation' started by mikez69, Oct 5, 2013.

  1. mikez69

    mikez69

    Joined:
    Oct 5, 2013
    Posts:
    6
    Let me start with, I don't know what information to share to get help, so please tell me what you need to know if you are interested in assisting.

    Now the problem. I am preparing a pretty complex series of animations and I hit my first roadblock. My current states are Idle, Sneak and Walk.

    It is WASD normal Walk movement and the SHIFT key is sneak while walking.

    If I press W, A, S, or D the character does not move. If I hold SHIFT and press a direction to walk he will begin to sneak.

    If he is sneaking and I continue to hold a direction and let go of the SHIFT key he will begin to walk normally.

    So it is apparent that for the most part all functionality is present but there seems to be one roadblock and I cannot find it.

    Any help will be greatly appreciated! Thanks!
     
  2. Archania

    Archania

    Joined:
    Aug 27, 2010
    Posts:
    1,662
    So you have it coded with the SHIFT-WASD but can't get it to work with the WASD keys.
    you should have it set within the update function that GetKeyDown.(Keycode.w) would play the forward animations.
    Are you using Mecanim or legacy?
    Reason I am asking is that the basic movement can be seen in the Unity Mecanim video.
     
  3. mikez69

    mikez69

    Joined:
    Oct 5, 2013
    Posts:
    6
    I am using Mecanim for this project. To be clear, WASD [Walk] only begins to function if I start with WASD+SHIFT [Sneak].

    The animator file is:

    [SNEAK] <---> [LOCOMOTION]

    ^------------[IDLE]-------------^

    The LOCOMOTION has an animation blend between run and walk.

    The script having to do with the player movement in part is:

    public class SO_PlayerMovement : MonoBehaviour


    {


    public float sprintSpeed;
    public float normalSpeed;
    public float sneakSpeed;
    public float playerSpeed;
    public float turnSmoother = 15f;
    public float run1;
    public float run2;
    public int hashchecker;




    private Animator moveAnim;
    private SO_Hashes hashHolder;


    void Awake ()
    {
    moveAnim = GetComponent<Animator>();
    hashHolder = GetComponent<SO_Hashes>();
    normalSpeed = 5f;
    sneakSpeed = 2.5f;
    sprintSpeed = 10f;
    hashchecker = hashHolder.fltSpeedHash;
    //moveAnim.SetLayerWeight(0,1);


    }

    void FixedUpdate ()
    //Calls every frame on rigidbodies to update their stuff. Using this to check for input.
    {
    hashchecker = hashHolder.fltSpeedHash;
    float x = Input.GetAxis("Horizontal");
    float z = Input.GetAxis("Vertical");
    run1 = x;
    run2 = z;
    bool bSneak = Input.GetKey(KeyCode.LeftShift); //need to actually set sneak button
    bool bSprint = Input.GetKey(KeyCode.Z); //need to actually set sprint button
    //SetCurrentSpeed(bSneak, bSprint);
    HandleMovement(x ,z , bSneak, bSprint);

    }

    void HandleMovement(float xAxis, float zAxis, bool isSneaking, bool isSprinting)
    //Handles all player movement factors: direction, rotation, speed, animation
    {
    //moveAnim.SetBool("Sneak", isSneaking); //Mike's Mod to allow proper usage
    moveAnim.SetBool(hashHolder.bSneakHash, isSneaking); //Mark's original script
    //moveAnim.SetBool(hashHolder.bSprintHash, isSprinting);
    // ^^^ Should activate sneaking or sprinting animations when appropriate

    if(xAxis != 0f || zAxis != 0f)
    {
    Vector3 newDirection = new Vector3(xAxis, 0f, zAxis);
    Quaternion newRotation = Quaternion.LookRotation(newDirection, Vector3.up);
    Quaternion incRotation = Quaternion.Lerp(rigidbody.rotation, newRotation, turnSmoother * Time.deltaTime);
    rigidbody.MoveRotation(incRotation);
    moveAnim.SetFloat("Speed", 5.7f, 0.1f, Time.deltaTime); //speed of animation



    }
    else
    {
    moveAnim.SetFloat("Speed", 0f);
    }
    }

    void SetCurrentSpeed(bool isSneaking, bool isSprinting)
    {
    if(isSneaking)
    {
    playerSpeed = sneakSpeed;
    }
    else if(isSprinting)
    {
    playerSpeed = sprintSpeed;
    }
    else
    {
    playerSpeed = normalSpeed;
    }
    }

    }
     
    Last edited: Oct 6, 2013
  4. mikez69

    mikez69

    Joined:
    Oct 5, 2013
    Posts:
    6
    Hmmm, anyone else out there have any thoughts?
     
  5. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    11,586
    I suggest isolating the problem to the state machine or the script:

    1. Temporarily remove the script from your character.
    2. Play the scene.
    3. Select your character in the Hierarchy.
    4. In the Animator window, manually set the parameters (Sneak, Speed) to make all the transitions and states work as expected.

    As you manually set the parameters, try to simulate what you would be testing with the script.

    If they don't work, the problem is in your Animator Controller.

    If they work, the problem is in your script.
     
unityunity