So I’ve seen tons of things for the WaitForSeconds function with coroutines but I just can’t figure out how to apply them here because even with the scripting API and all the questions on here I can’t figure it out. I’m trying to make it so when my character dies their meshrenderer is turned off and they instantiate a particle effect before finally changing the characters transform to equal an empty GO called “spawnPoint”. I have this working but I want there to be 2 second delay between when the meshrenderer is turned off the the teleportation so you can see the particle effect before starting over but I can’t figure out how to make this work. (quick side note: I know my code is probably hideous at the moment but I just want to make this work before I fix other issues) Here’s the code I’ve written so far:
using UnityEngine;
using System.Collections;
public class PlayerMoveScript : MonoBehaviour {
public float hor;
public float speed;
public float jumpSpeed;
public bool canJump = true;
public Transform spawnPoint;
public GameObject deathParticle;
public Rigidbody rb;
public GameObject cubeScoot;
public GameObject cylinderScoot;
void Start () {
rb = GetComponent<Rigidbody>();
}
void Update () {
hor = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
//ver = Input.GetAxis("Vertical") * speed * Time.deltaTime;
transform.position += new Vector3(hor, 0, 0);
}
void FixedUpdate() {
if (Input.GetKeyDown(KeyCode.Space) && canJump == true)
{
rb.AddRelativeForce(Vector3.up * jumpSpeed);
canJump = false;
// if (transform.position.y >= transform.position.y + 3.5f) {
// rb.AddRelativeForce(Vector3.down * jumpSpeed);
// }
}
}
void OnCollisionEnter(Collision other) {
if(other.gameObject.CompareTag("floor"))
canJump = true;
}
void OnTriggerEnter(Collider trigger) {
if (trigger.gameObject.CompareTag("Trap")){
PlayerDeath();
}
}
IEnumerator PlayerDeath(){
cubeScoot.GetComponent<MeshRenderer>().enabled = false;
cylinderScoot.GetComponent<MeshRenderer>().enabled = false;
yield return new WaitForSeconds(2);
transform.position = spawnPoint.transform.position;
cubeScoot.GetComponent<MeshRenderer>().enabled = true;
cylinderScoot.GetComponent<MeshRenderer>().enabled = true;
Instantiate(deathParticle, transform.position, Quaternion.identity);
}
}