- Home /
Wait for animation to finish
Hi!
I have a script for a funfair game i'm working on, and I cant figure out how can let the animation fully finish before the user can press the same key again.
I hope someone can help me with this.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
public class SafetyBar : MonoBehaviour
{
public Animator animator;
public bool IsClosed { get; private set; } = true;
public Gondola gondola;
public Arm arm;
public Floor floor;
public Text label;
private float exectureActionTime = 0;
private Action scheduledAction = null;
private float lastAnimation = 0;
void Start()
{
animator = GetComponent<Animator>();
}
void Update()
{
if (label != null)
label.text = IsClosed ? "closed" : "open";
if (Input.GetKeyDown(KeyCode.B))
{
if (!IsClosed || (!floor.IsDown && !arm.IsMoving() && !gondola.IsMoving()))
{
exectureActionTime = Time.time + UnityEngine.Random.Range(0.0f, 1.0f);
scheduledAction = () =>
{
IsClosed = !IsClosed;
lastAnimation = Time.time;
if (IsClosed)
{
animator.Play("BeugelOmlaag");
}
else
{
animator.Play("Beugel");
}
};
}
}
if (exectureActionTime != 0 && scheduledAction != null && Time.time > exectureActionTime)
{
scheduledAction();
exectureActionTime = 0;
scheduledAction = null;
}
}
}
Answer by LoloBad · Feb 09, 2021 at 09:50 AM
Why not using an Animaton Event at the very last frame of the animation ?
When you trigger the animation you "lock" your GameObject, at the end of the animation the Animation Event unlock the object.
The Update() method should look like :
void Update()
{
if (!locked)
{
if (Input.GetKey(KeyCode.Space))
{
GetComponent<Animator>().SetTrigger("my-animation");
}
}
}
And the Animtion Event is :
Where the Unlock method is:
public void Unlock ()
{
locked = false;
}
Answer by RodrigoAbreu · Feb 08, 2021 at 11:16 PM
Hi @verschurengiovanni, you can use animation events for that. https://docs.unity3d.com/Manual/script-AnimationWindowEvent.html
Or you can manually create wait in a coroutine:
yield return new WaitForSeconds(anim.GetCurrentAnimatorStateInfo(0).length+anim.GetCurrentAnimatorStateInfo(0).normalizedTime);
I'm not sure how I can implement this correctly, still learning :)
Gotcha, check it out here in this example. https://www.youtube.com/watch?v=INJmqquHaz4
Alright, so I got this now
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
public class SafetyBar : $$anonymous$$onoBehaviour
{
public Animator animator;
public bool IsClosed { get; private set; } = true;
public Gondola gondola;
public Arm arm;
public Floor floor;
public Text label;
private float exectureActionTime = 0;
private Action scheduledAction = null;
private float lastAnimation = 0;
void Start()
{
animator = GetComponent<Animator>();
}
void Update()
{
if (label != null)
label.text = IsClosed ? "closed" : "open";
if (Input.GetKeyDown(KeyCode.B))
{
if (!IsClosed || (!floor.IsDown && !arm.Is$$anonymous$$oving() && !gondola.Is$$anonymous$$oving()))
{
exectureActionTime = Time.time + UnityEngine.Random.Range(0.0f, 1.0f);
scheduledAction = () =>
{
IsClosed = !IsClosed;
lastAnimation = Time.time;
if (IsClosed)
{
StartCoroutine(BeugelOmlaag());
}
else
{
StartCoroutine(Beugel());
}
};
}
}
if (exectureActionTime != 0 && scheduledAction != null && Time.time > exectureActionTime)
{
scheduledAction();
exectureActionTime = 0;
scheduledAction = null;
}
}
IEnumerator BeugelOmlaag()
{
animator.Play("BeugelOmlaag");
yield return new WaitForSeconds(animator.GetCurrentAnimatorStateInfo(0).length + animator.GetCurrentAnimatorStateInfo(0).normalizedTime);
}
IEnumerator Beugel()
{
animator.Play("Beugel");
yield return new WaitForSeconds(animator.GetCurrentAnimatorStateInfo(0).length + animator.GetCurrentAnimatorStateInfo(0).normalizedTime);
}
}
But I can still press "B" again fore the animation finishes. Am I missing something?
Your answer
Follow this Question
Related Questions
Trying to find the highest number than add it to itself. 2 Answers
IEnumerator Loop on keypress 1 Answer
Move from A to B after recieving touch input 1 Answer
Need help fixing my script 1 Answer
Slowly increase motor.force 1 Answer