Reputation: 36351
I created a class called AnimationController
, and I am having issues with it. When I press space
it triggers the Jump
animation in the Animator
but it it plays twice. I can not figure out why it is doing this. What is a better way to deal with this?
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Animator))]
public class AnimationController : MonoBehaviour {
Animator animator;
AnimatorStateInfo baseLayer;
void Start() {
animator = GetComponent<Animator>();
baseLayer = animator.GetCurrentAnimatorStateInfo(0);
}
void Update () {
if(Input.GetButton("Jump") && !baseLayer.IsName("Jump")) {
animator.SetTrigger("Jump");
}
}
}
Upvotes: 1
Views: 4981
Reputation: 5982
I'm very new to Unity, but I suspect that this is because you're using GetButton
instead of GetButtonDown
or GetButtonUp
.
From the documentation, for GetButton
:
Returns true while the virtual button identified by buttonName is held down.
So, if you're holding the spacebar for two frames (even though you've pressed it once, the physical action takes longer than one frame) then it will fire twice. If you kept it held down for longer than that, it would keep firing.
If you use GetButtonDown
or GetButtonUp
instead, it should only fire once, for the exact frame where the press is registered.
Returns true during the frame the user pressed down the virtual button identified by buttonName.
Upvotes: 4