How do I respawn after falling below a certain spawn position?
So I am making a parkour game and I want to make it so that once I fall below Y 0 I go back to the first platform and I have to restart. I do not even know where to begin, I know it has to do something with getting the position of the character. Here is my code for my move script, I want to put this new code into the move script.
public class Move : MonoBehaviour { private CharacterController controller; private Vector3 playerVelocity; private bool groundedPlayer; public float playerSpeed = 2.0f; public float jumpHeight = 1.0f; public float gravityValue = -9.81f; public float JumpPad = 50;
private void Start()
{
controller = gameObject.AddComponent<CharacterController>();
}
void OnCollisionEnter(Collision other)
{
if (other.gameObject.CompareTag("JumpPad"))
{
jumpHeight += jumpHeight * 3;
}
}
void Update()
{
groundedPlayer = controller.isGrounded;
if (groundedPlayer && playerVelocity.y < 0)
{
playerVelocity.y = 0f;
}
Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
controller.Move(move * Time.deltaTime * playerSpeed);
if (move != Vector3.zero)
{
gameObject.transform.forward = move;
}
// Changes the height position of the player..
if (Input.GetButtonDown("Jump") && groundedPlayer)
{
playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
}
playerVelocity.y += gravityValue * Time.deltaTime;
controller.Move(playerVelocity * Time.deltaTime);
}
}
Answer by TheIrishKraken · May 16 at 05:06 AM
One way you could do this is create an empty game object called Spawn Point & tag it as "SpawnPoint". Create a prefab of your player gameobject. Put this in the scene where you want your player to respawn. Create a new script called SpawnPlayer and attach it.
Inside your script above reference the SpawnPlayer script and call it spawnPlayer for example. Find the script using : spawnPlayer = GameObject.FindGameObjectWithTag("SpawnPoint").GetComponent(); Then inside your update method include the below code to check if the Players position is less then 0 Destroy it if so Create a new player and position it at the Spawn Point.
Hope this is clear.