Animation not playing properly on key press
Hi,
I've searched various questions/answers on here but none seem to address my issue.
I'm trying to make a horse walk when you press the up arrow and then after 3 seconds of holding down the up arrow make it run. All of this functionality works however the animation doesn't seem to and it just gets stuck on the first frame. I'm unsure why this is happening!
public class PlayerController : MonoBehaviour {
private Rigidbody rb;
public float speed;
public Animator anim;
public float timeToRun = 3.0f;
public float runTimer = 0;
void Start() {
rb = GetComponent<Rigidbody>();
anim = GetComponent<Animator>();
}
void Update()
{
if (Input.GetKey("up") || Input.GetKey("w") )
{
anim.Play("Horse_Walk", -1, 0f);
transform.position += transform.forward * Time.deltaTime * 2.5f;
runTimer += Time.deltaTime;
if (runTimer > timeToRun)
{
anim.Play("Horse_Run", -1, 0f);
transform.position += transform.forward * Time.deltaTime * 5.0f;
}
}
}
Again, the functionality of the horse moving is fine, it's just the animation which doesn't seem to work properly? Any suggestions?
Your answer
Follow this Question
Related Questions
How can I animate my 3D object without affecting the physics? 0 Answers
how to make an animation to play when a int has a certain value, and pressing space key? 0 Answers
Unity and Android input touch? 1 Answer
GetButtonDown for joystick? 1 Answer
How to make a player input a sequence to activate a door/trigger? 0 Answers