Hi, i recently got my map to respawn the player at a point when he dies(or hits a trigger), I wanted to put in checkpoints now so the player doesnt have to restart all the way at the beginning, so far i have this, courtesy of Aldo.
var dest: Transform; // drag the destination object here
var sound: AudioClip; // define a teleport sound, if you want
function OnTriggerEnter(other: Collider){
if (other.tag == "Player"){
// move the player and align it to the dest object:
other.transform.position = dest.position;
other.transform.rotation = dest.rotation;
I get the logic behind how to do it, just not sure about the code
I have an object for spawning my player. It writes its position in the character’s main script and when the player dies, he is moved to that position. Changing that position would be the simple matter of writing the new object’s position in the same location, when the player enters the trigger.
I used a collision event for I found the trigger event to be very specific in how the condition is met. here’s some functions that I used and they’re pretty basic.
public class Checkpoint : MonoBehaviour
{
Vector3 spawnlocation;
void Start ()
{
var tran = this.GetComponent<Transform>();
spawnlocation =new Vector3(tran.position.x,tran.position.y,0);
}
void OnCollisionEnter(Collision other)
{
var tran = this.GetComponent<Transform>();
if (other.gameObject.name == "Player")
{//Sends a message to the player to call the UpdateSpawn function giving it the spawnlocation variable
other.gameObject.SendMessage("UpdateSpawn", spawnlocation, SendMessageOptions.DontRequireReceiver);
//moves the checkpoint out of the way so it can’t be collided with again
tran.position = new Vector3(tran.position.x, tran.position.y, 1);
}
}
}
Player:
Vector3 Spawnpoint = new Vector3(0, 1, 0);