- Home /
How do I make my player re-spawn in its original place on collision of enemy?
How do I make my player re-spawn in its original place on collision of enemy? I have set this piece of code on to the enemy but my player is not respawning?
Thanks in advance, Dil
#pragma strict
var Player:Transform;
var spawn:Transform;
function OnCollisionEnter(collision:Collision)
{
if(collision.gameObject.tag == "Player")
{
Destroy(collision.gameObject);
}
Player.transform.position = spawn.position;
}
Comment
Answer by RafaelCN · Mar 13, 2014 at 05:56 PM
First you have to grab the coordinates(position) of the player in the world
public Vector3 originalPosition = transform.position;
Then, when he collides instead of destroying the object move the player to the originalPosition
, or, destroy the game object, and then create one game object on the originalPosition
:
function OnCollisionEnter(collision:Collision)
{
if(collision.gameObject.tag == "Player")
{
Destroy(collision.gameObject);
Instantiate(Object theObject, Vector3 originalPosition, Quaternion rotation);
}
}
I don't know if it'll work because i just wrote the answer, make a test a mark as correct if it is :D.
Regards.