- Home /
Checkpoint system???
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
if(player hits trigger)
{
change dest to "new trigger object here"
}
Answer by Piflik · Apr 16, 2012 at 05:37 PM
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.
function OnTriggerEnter(item : Collider) {
if(item.tag == "Player")
item.GetComponent("PlayerScript").startPos = transform.position;
}
It is a different script than yours, but so far it works in my game...and you wouldn't have to store the destination object in every checkpoint ;)
If you want to keep the code you have, you would have to make something like this:
var dest: Transform; // drag the destination object here
function OnTriggerEnter(item : Collider) {
if(item.tag == "Player")
dest.position = transform.position;
}
Haven't tested this, so say if it doesn't work.
it didnt work, i messed around with it as well and I had no luck
Answer by Alpha_Guac · Oct 30, 2015 at 09:12 PM
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);
void UpdateSpawn(Vector3 position)
{
Spawnpoint = position;
}
This way the Player deals with the spawn location nothing else. I hope this helps you out
thats c sharp not java script xd everybody else speaking about js and u c sharp xd
Answer by santiandrade · Nov 19, 2015 at 12:29 PM
I have just writen a post about it in my blog: https://santiandrade.github.io/Unity-Creating-a-checkpoints-system/
Regards!
Your answer
Follow this Question
Related Questions
Need help with a checkpoint system 0 Answers
enable / disable particle system 1 Answer
How do you make a respawn and checkpoint script? 0 Answers
Particle System rotate 2 Answers
My respawn system does not work 1 Answer