- Home /
Respawning my AVATAR
Hi there, I have a script spawning my player at the beginning of the game.
When my player dies ( when falling from more than 8 meters ).
My respawn is not working.
Any idea to help me ?
Thank you...
Here is my script:
public class DeathRecognition : MonoBehaviour
{
public Transform spawnPoint;
public bool falling = false;
public float lastY; // last grounded height
public float hauteurChute; // ce ki cause ma mort
public CharacterController character; // sera eventuellement detruit
private float deathFall = 7.632f; // pour etre constant de mon 8 m de map...
// lavatar glisse un peu sur les parois (+/- 0.30)
void spawn()
{
transform.position = spawnPoint.position;
}
void Start()
{
spawn();
}
void Update()
{
if (character.isGrounded == false) // est en train de tomber sans avoir saute
{
hauteurChute = lastY - transform.position.y ;
if (hauteurChute < 0) lastY = this.transform.position.y; // si je suis en train de sauter
// la valeur la + haute de mon saut
}
else
{
if ( hauteurChute > 0.5 )
{
Debug.Log("Hauteur du saut :"+ hauteurChute); // hauteur de mon saut
}
if ( hauteurChute >= deathFall )
{
Debug.Log("DEAD");
Die();
}
hauteurChute = 0.0f; // si ma chute n<est pas mortelle
lastY = this.transform.position.y; // je reinitialise ma chute a 0
}
}
void Die()
{
Destroy(gameObject);
spawn();
}
}
Answer by syclamoth · Mar 06, 2012 at 02:25 PM
Basically, the problem is here:
void Die()
{
Destroy(gameObject);
spawn();
}
This will first destroy the object, and then call 'spawn' which moves the transform back to the spawnPoint.
However, because the object gets destroyed first, it won't work! The object gets moved to the spawn point, and then disappears before the next frame begins.
If you just want the player to move back to the spawn, remove the 'Destroy' line.
If you want it to create a new player after destroying the old one, try instantiating a prefab, or in fact calling
Instantiate(gameObject);
which will create a perfect copy of the character.
By doing this, more than one player comes back to life meaning it is not working :(
What's creating the second one? I meant you should either instantiate a copy or remove the Destroy line, not both!
Your answer
Follow this Question
Related Questions
How do I randomize the order of spawn points without repeats? 1 Answer
Gameobject Respawn 1 Answer
Player Respawn After Death 1 Answer
Array index out of Range 1 Answer