- Home /
Question by
Johnz1234 · Apr 08, 2015 at 10:46 AM ·
scenemobilescriptingbasics
Player Respawn affter 3 scenonds?
here is my script i want to know how to make him respawn affter 2 seconds?
using UnityEngine; using System.Collections;
public class LevelManager : MonoBehaviour {
public GameObject currentCheckpoint;
private PlayerController player;
// Use this for initialization
void Start () {
player = FindObjectOfType<PlayerController> ();
}
// Update is called once per frame
void Update () {
}
public void RespawnPlayer()
{
Debug.Log ("Player Respawn ");
player.transform.position = currentCheckpoint.transform.position;
}
}
Comment
The simplest would probably be along the lines of:
private float delay = 3.0f;
private float timeofdeath;
public void Hesdeadjim()
{
timeofdeath = Time.time;
}
void Update()
{
if(timeofdeath+delay <= Time.time) RespawnPlayer();
}
Answer by _joe_ · Apr 08, 2015 at 11:16 AM
Don't work in Update... The fastest way is to use Invoke.
In Start()
call
Invoke("RespawnPlayer", 3);
If you want to use Coroutines, change your RespawnPlayer to :
IEnumerator void RespawnPlayer()
{
yield return new WaitForSeconds(3);
Debug.Log ("Player Respawn ");
player.transform.position = currentCheckpoint.transform.position;
}
and call it from Start():
StartCoroutine("RespawnPlayer");