Respawn from falling off world!

If you fall of the side of the world whats the script you need for you to teleport back to spawn point?

make a new c# script and name it respawn, copy and paste this >>

using UnityEngine;
using System.Collections;

public class respawn : MonoBehaviour {
	public float threshold;

	void FixedUpdate () {
		if (transform.position.y < threshold)
			transform.position = new Vector3(0, 0, 0);
	}
}

then drag that script onto your character…the primary parent object. THEN go to the inspector and set the threshold to something below the floor…on a standard setup a number like -20 or -50 will work great.

float fallZone = -10f;
public Transform playerSpawnPoint = null; //create an empty gameobject and assign it to this script in the inspector

void Update(){

if(player.transform.position.y < fallZone) //Assuming its a 2D game
{
 player.transform.position = playerSpawnPoint.position;
}
}

public Transform spawnPoint;//Add empty gameobject as spawnPoint
public float minHeightForDeath;
public GameObject player; //Add your player

 void Update()
{
  if(player.transform.position.y < minHeightForDeath)
      player.transform.position = spawnPoint.position;
}

//There are many ways to do this. This is just one way
//Try not to use Physics if you don't need it(there is a way to 
//do this by using Physics)

suppose the platform size is 300 on the x axis , y axis and anything on z axis

// EndGame() is a function defined by me to end game on falling out of the platform stored in a script called GameManager

//

public Rigidbody player;

public float lastposition;

public GameObject gameover;

bool gameHasEnded = false;

public void Resume()

{

if (player.position.y < 0.1f) // player dies if falls below 0.1 on y axis

{

lastposition = player.position.z;

FindObjectOfType ().EndGame(); .

transform.position = new Vector3 (0, y ,lastposition);

}}

public void EndGame ()

{

if (gameHasEnded == false)

{

gameHasEnded =true;

respawn ();

gameover.SetActive(true);

}

// THIS SCRIPT HAS TO BE ATTACHED TO THE PLAYER
By default it will set the offset to be -50 on the y from the starting position

using UnityEngine;
public class PlayerRespawn : MonoBehaviour {

Vector3 _InitialPos; // Starting position of the player
float _YOffset;

void Start () {
	_InitialPos = transform.position;
	_YOffset = _InitialPos.y - 50;
}

void Update () {
	if (_InitialPos.y < _YOffset)
	{
		transform.position = _InitialPos;
	}
}

}

//Add A box collider underneath your world. With the Tag “Death”

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 
 public class DeathReSpawn : MonoBehaviour
 {
     public Transform spawnPosition;
     Rigidbody body;
 
     Void Start()
     {
         GetComponent<Rigidbody>();
     }
     Void OnTriggerEnter(Collider other)
     {
         if (other.tag == "Death")
         {
             transform.position = spawnPosition.position;
             body.velocity = Vector3.zero;
         }
     }
 }