- Home /
Unable to re-spawn after colliding with enemy
Hi I am new to unity, I have written a re-spawn which works if the character falls off the edge of the map, however once the player collides with the enemy the re-spawn text will appear and the game will stop, however once I press enter nothing happens and the player is stuck. Any help would be great thanks :)
Here is my code:
onTriggerEnter() { if(other.gameObject.tag == "enemy") { deadText.text = "You died Press Enter to Continue"; Respawn(); }
}
void Respawn() { Time.timeScale = 0.0f;
if(Input.GetKey("enter"))
{
Time.timeScale = 1.0f;
deadText.text = "";
transform.position = transform.position = new Vector3 (0f, 2f, 0f);
}
}
Answer by RudyTheDev · Jan 05, 2014 at 08:39 PM
That's because if(Input.GetKey("enter"))...
only happens once, inside your Respawn()
function, which is only called once in onTriggerEnter()
. That if statement only checks for the return button at the exact frame the trigger happens. After that, the game is "paused" and Respawn()
is never called again.
To solve this, you need to remember that the player died. There are literally hundreds of ways to do this, depending on your game. The easiest way is to probably to make something like
private bool playerDead = false;
private void onTriggerEnter()
{
if (other.gameObject.tag == "enemy")
Die();
}
private void Update()
{
if (playerDead)
if (Input.GetKey("enter"))
Respawn();
}
private void Die()
{
Time.timeScale = 0.0f;
deadText.text = "You died Press Enter to Continue";
playerDead = true;
}
private void Respawn()
{
Time.timeScale = 1.0f;
deadText.text = "";
transform.position = new Vector3(0f, 2f, 0f);
playerDead = false;
}
Your answer
Follow this Question
Related Questions
How to use collision.name in custom function 1 Answer
Fist Punch Collision 0 Answers
Distribute terrain in zones 3 Answers
Object moving too fast, so that the collider does not work (C#) 2 Answers