- Home /
help with checkpoint sytem C#
trying to instantiate an my player when the level starts at a object location this is and when i hit the checkpoint it moves the spawnpoint location so if the player dies i can hit gameover screen then loop round to checkpoint instead of starting again i know i could just create a new scene but would bulk out the project to much
so i thought if i spawn player at start of level at an empty gameobject spawn point then when the checkpoint is hit it moves the spawnpoint to new location then when the level resets the player spawns there heres what ive got so far any help would be great
using UnityEngine; using System.Collections;
public class Checkpoint : MonoBehaviour {
public GameObject CheckP;
public Vector3 CheckPos;
public GameObject player;
public Transform SpawnPosition;
public GameObject SpawnPoint;
void awake(){
DontDestroyOnLoad(SpawnPoint);
}
void start(){
Instantiate (player,SpawnPosition,Quaternion.identity);
}
i need way to move spawn ontrigger and a way to make the transform a vector 3 thnx
void OnTriggerEnter2D (Collider2D other)
{
if (other.tag == "Player")
Instantiate(CheckP, CheckPos, Quaternion.identity);
Debug.Log ("hit");
}
} i need a way to transform the spawnpoint on trigger and a way to make the spawn position transform a vector 3 ive tried the unity docs but it want numbers for public vector 3 and transform is not accepted in the instantiate :P
Why do you need "a way to make the spawn position transform a vector 3" if you're using 2D Triggers? Isn't your game 2D?
it doesnt really its just when i type instantiate it says it needs a gameobject ,vector3, Quaternion.identity so thats what ive been using but ur right it is 2d so if there is other way please let me know
If ins$$anonymous$$d of destroying the player object, you just disabled and then reset location (Vector2) to last checkpoint, you would not need to instantiate anyway, which would then remove the problem wouldn't it?
Answer by Haythamcrc · Nov 15, 2014 at 05:31 PM
1- Create empty gameobject name it --- SpawnPoint 2- Create another gameObject name in --- CheckPoint
Add this Script to CheckPoint :
using UnityEngine;
using System.Collections;
public class CheckPointScript : MonoBehaviour
{
public Transform SpawnPoint;
void OnTriggerEnter2D(Collider2D col)
{
if(col.tag =="Respawn")
{
SpawnPoint.position = new Vector3(transform.position.x, transform.position.y, SpawnPoint.position.z);
}
}
}
3- Add Box Collider 2D to CheckPoint with Is Trigger.
I hope this will help you :)