Respawn point
So i have made myself a respawn script,problem is that when i trigger it ,my player jumps to that position locks in place,and it takes some seconds before it gets freed. here's the code i put togheter;
using UnityEngine;
using System.Collections;
public class IFIHADACAT : MonoBehaviour {
public Vector3 spawnpoint;
public float life;
public bool respawn;
public Vector3 checkpoint;
// Use this for initialization
void Start () {
life = 1;
respawn = false;
}
void RespawnPlayer () {
transform.position = spawnpoint;
}
void OnCollision2D (Collider2D col){
if (col.gameObject.tag == "CheckPoint")
spawnpoint = checkpoint;
}
// Update is called once per frame
void Update () {
if (life == 0) {
respawn = true;
}
if (respawn) {
StartCoroutine (riperino ());
}
}
IEnumerator riperino() {
Debug.Log ("ded");
yield return new WaitForSeconds (4);
Debug.Log ("ugh,you again?");
RespawnPlayer ();
respawn = false;
}
}
P.s. i also want to move my spawn point to my check point but i don't know why it doesn't work.Thanks in advance.
Answer by awest · Jun 21, 2016 at 05:33 PM
You are starting your Coroutine every frame that your lives are 0 and respawn is true, but you don't set respawn to false for after 4 seconds. The method starts every frame for those 4 seconds. It should look like this.
IEnumerator riperino() {
Debug.Log ("ded");
respawn = false;
yield return new WaitForSeconds (4);
Debug.Log ("ugh,you again?");
RespawnPlayer ();
}
As far as the checkpoint goes I assume you're setting it in the inspector. Is the collision being detected? Make sure the tag is exact and that one object has a Rigidbody2D.
Also I don't see where you give the player life back. That may be another issue.
Your answer
Follow this Question
Related Questions
How Checkpoint works? 1 Answer
How to make player respawn without resetting scene 1 Answer
How do i put "Wasted!" after respawn? 0 Answers
Respawn not working past 2nd respawn 1 Answer
Platformer multiple checkpoints help 0 Answers