- Home /
Question by
MrTeddyBearr · Oct 20, 2018 at 04:34 PM ·
animationunity 5scripting problem
Why isn't my animation state changing properly?
This is the code I have in the controller:
> using System.Collections; using
> System.Collections.Generic; using
> UnityEngine;
>
> public class PlayerController :
> MonoBehaviour { public Rigidbody rb;
> public Animator anim; public float
> baseSpeed; float sprintSpeed; bool
> isSprint;
>
> private void Start(){ rb =
> GetComponent<Rigidbody> (); anim =
> GetComponent<Animator> ();
> sprintSpeed = 1; }
>
> private void Update(){ if
> (Input.GetKeyDown (KeyCode.LeftShift))
> { isSprint = true; sprintSpeed =
> 2; } else { isSprint = false;
> sprintSpeed = 1; } }
>
> private void FixedUpdate(){ float
> moveHorizontal = Input.GetAxis
> ("Horizontal"); float moveVertical =
> Input.GetAxis ("Vertical"); Vector3
> movement = new
> Vector3(moveHorizontal,0,moveVertical);
> transform.rotation =
> Quaternion.LookRotation(movement);
>
> if (isSprint) { anim.SetBool
> ("isSprint", true); } else if
> (rb.velocity.magnitude > 0 &
> !isSprint) { anim.SetBool
> ("isSprint", false);
> anim.SetBool("isWalking",true); }
> else { anim.SetBool ("isSprint",
> false);
> anim.SetBool("isWalking",false);
> }
> rb.AddForce (movement * baseSpeed * sprintSpeed);
> transform.Translate (movement *
> Time.deltaTime, Space.World); } }
When I press shift I'm expecting the character to switch to the sprint state. However it doesn't seem to be registering when I press shift, it never shifts state. Have I done something wrong?
Comment
Best Answer
Answer by MrTeddyBearr · Oct 20, 2018 at 04:44 PM
Figured it out....in case it helps anyone:
Had to change:
Input.GetKeyDown
To:
Input.GetKey
As otherwise it checks every frame.