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;
}
}
}