- Home /
Answer by abhi_360 · May 22, 2015 at 05:05 AM
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)
Answer by jka4kcha · May 22, 2015 at 09:15 AM
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.
I had to edit this code to make it work, how did I make it work? I changed the coordinates of the new Vector3
.
Change the 0, 0, 0
into your coordinates where your player will respawn at. Anyway, this code helped a lot.
Answer by chintan_shroff · May 22, 2015 at 09:20 AM
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;
}
}
Answer by anurag7991 · Jul 27, 2018 at 06:17 PM
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);
}
Answer by Sazails · Jul 27, 2018 at 06:24 PM
// 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;
}
}
}
Your answer
Follow this Question
Related Questions
OnTriggerEnter2D/OnCollisionEnter2D - delay 1 Answer
My spikes don't damage the player 2 Answers
Character Fails To Jump Sometimes 1 Answer
Do triggers not fire if spawned colliding? 1 Answer
Collision triggers do not work on child gameobject 3 Answers