- Home /
Need help with respawn after falling.
In my game when the character falls he just keeps falling... I would like a code to put into a large box under the map that will reset the location of my character when the character collides with it. I am a complete noob so nothing to complicated please.
Answer by micahhauge · Dec 07, 2011 at 10:58 PM
Thanks for replying so quick. I made the box a collider but i don't know how to make a "transform". How would i do so?
Please add comments ins$$anonymous$$d of creating new answers. And don't forget to mark your question as answered once you have what you are looking for.
Answer by syclamoth · Dec 07, 2011 at 06:39 PM
Make the box a trigger collider, make an empty transform at the top for the respawn point, and then use this-
void OnTriggerEnter(Collider other)
{
other.transform.position = respawnPoint.position;
if(other.rigidbody)
{
other.rigidbody.velocity = Vector3.zero;
}
}
Do I attach this script to the player or the box collider? If it is for the player, do I just add to the script?
Answer by rabbitfang · Dec 07, 2011 at 11:46 PM
A trigger box can be unreliable because the player may over shoot the box, or may pass through it with out the collision being detected (for whatever reason). While if you get it big enough, it should pretty much always work, you have an issue if your map gets bigger and you forget to update the box.
I would attach a script to the character that respawns him/her/it when it passes a certain z-value.
var respawnHeight : float = 50;
function FixedUpdate() {
if (transform.position.z < respawnHeight) {
DoRespawn();
}
}
Modify respawnHeight
to be the value at which the player will respawn. Obviously you need to implement DoRespawn()
yourself.