I have looked at many articles and I’m just very confused. I would like my coinPrefab to be instantiated after a 2 second delay once the “Is Being Opened” parameter in the Animator is set to true. I have attempted IEnumerator Coroutines but it feels very much over my head right now. Please help!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Chest : MonoBehaviour {
[SerializeField] private Rigidbody coinPrefab;
[SerializeField] private Transform spawner;
[SerializeField] private float delay = 3f;
private Animator anim;
private bool interactable = false;
private void Start() {
anim = GetComponent<Animator>();
}
private void Update() {
if(interactable && Input.GetKeyDown(KeyCode.Space)) {
OpenChest();
}
}
private void OnTriggerEnter(Collider other) {
if (other.gameObject.tag == "Player")
interactable = true;
}
private void OnTriggerExit(Collider other) {
if (other.gameObject.tag == "Player")
interactable = false;
}
private void OpenChest() {
anim.SetBool("Is Being Opened", true);
yield WaitForSeconds(2f);
Rigidbody coinInstance = Instantiate(coinPrefab, spawner.position, spawner.rotation);
}
}