So the animation lasts about .5 secs, and I want there to be a delay between the animations. here are the scripts I need to do this for:
Slide:
function Update () {
if (Input.GetButtonDown("Fire1")) {
var gunSound : AudioSource = GetComponent.<AudioSource>();
gunSound.Play();
GetComponent.<Animation>().Play("slideAnimation");
}
}
Recoil:
function Update () {
if (Input.GetButtonDown("Fire1")) {
GetComponent.<Animation>().Play("gunRecoil");
}
}
Shoot:
function Update () {
if (Input.GetButtonDown("Fire1")) {
GetComponent.<Animation>().Play("bulletShoot");
}
}
EDIT - Correcting Answer to Reflect New Info About the Problem
So this is actually much simpler than the solution proposed below. I’d still recommend moving the input handling to a separate component with references to the other scripts. Then it just tracks the amount of time elapsed since the last time the Fire1 button was pressed, and triggers animations based on that condition.
In Code:
public float MinTimeBetweenShots = .5f; // Exposed to Editor
private float TimeSinceLastShot;
void Awake () {
TimeSinceLastShot = MinTimeBetweenShots;
}
void Update () {
if (Input.GetButtonDown ("Fire1") && CanShoot()) {
Shoot ();
}else{
TimeSinceLastShot += Time.deltaTime;
}
}
bool CanShoot () {
return TimeSinceLastShot >= MinTimeBetweenShots;
}
void Shoot () {
TimeSinceLastShot = 0f;
m_SlideComponent.PlayAnimation();
m_ShootComponent.PlayAnimation();
m_RecoilComponent.PlayAnimation();
}
Old Answer - Corrected Above
First off, I’m assuming that Slide, Recoil and Shoot are separate components on the same object.
Second, I’m also assuming that you want to play each animation one after another.
If that’s not the case, let me know how this is structured and I’ll update my answer.
With this method, I’d recommend a Coroutine.
Basically you’d remove the input code from each function, and rename Update()
to PlayAnimation()
. Input would be handled by another component that has references to the Slide, Recoil and Shoot components. That component’s update would look like:
void Update () {
if (Input.GetButtonDown ("Fire1")) {
StartCoroutine (PlayAnimations());
}
}
IEnumerator PlayAnimations () {
m_SlideComponent.PlayAnimation();
yield return new WaitForSeconds (.5f); // example delay length
m_ShootComponent.PlayAnimation();
yield return new WaitForSeconds (.5f);
m_RecoilComponent.PlayAnimation();
}
You can see more about Coroutines here
But this isn’t really the way this should be handled, and I’d recommend looking into Animator Controllers here.