What I am trying to do is once the player dies he respawns to the beginning of the level. However, once the player respawns, it just keeps spawning over and over again and does not let me do anything. I am guessing that this because it is in the FixedUpdate() function. Is there any way to call the Die() function only once?
Here is my code:
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public float MovementSpeed;
private Vector3 input;
private float maxSpeed = 5f;
private Vector3 spawn;
public GameObject deathParticles;
public GameManager manager;
public AudioClip[] audioClip;
public bool usesManager = true;
private static bool Die = false;
private bool checkpointEnabled = false;
private Vector3 checkpoint;
private Vector3 respawn;
void Start () {
spawn = transform.position;
checkpoint = gameObject.transform.position;
respawn = spawn;
if (usesManager)
{
manager = manager.GetComponent<GameManager>();
}
}
void FixedUpdate () {
transform.Rotate(0,Input.GetAxis("Rotate")*60*Time.deltaTime,0);
input = new Vector3(Input.GetAxisRaw("Horizontal"),0,Input.GetAxisRaw("Vertical"));
if(Die)
{
Instantiate(deathParticles, transform.position,Quaternion.Euler(0,0,0));
transform.position = spawn;
}
if (checkpointEnabled)
{
respawn = checkpoint;
}
if (endLevel.isEndLevel)
{
respawn = spawn;
}
if(GetComponent<Rigidbody>().velocity.magnitude < maxSpeed)
{
GetComponent<Rigidbody>().AddRelativeForce(input * MovementSpeed);
}
if (transform.position.y < -2)
{
Die = true;
}
if (Die == true & checkpointEnabled == true)
{
transform.position = respawn;
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Checkpoint")
{
checkpointEnabled = true;
Debug.Log("setCheckpoint: Player Hit Checkpoint.");
}
if (other.GetComponent<Collider>().tag == "Token")
{
if (usesManager)
{
manager.tokenCount +=1;
}
PlaySound(0);
Destroy(other.gameObject);
}
if(other.GetComponent<Collider>().tag == "Enemy")
{
Die = true;
PlaySound(2);
}
if(other.GetComponent<Collider>().tag == "Goal")
{
manager.CompleteLevel();
Time.timeScale = 0f;
PlaySound(1);
}
}
void PlaySound(int clip)
{
GetComponent<AudioSource>().clip = audioClip[clip];
GetComponent<AudioSource>().Play();
}
}