- Home /
How t o make sure the enemy is spawn at specific time?
Hi guys im able to spawn the enemy like i define but it just keep on spawning non-stop how do i define it properly?
if (Time.time <= 1.0f) {
// spawn
GameObject g = (GameObject)Instantiate(teddy, transform.position, Quaternion.identity);
// get access to the navmesh agent component
NavMeshAgent n = g.GetComponent<NavMeshAgent>();
n.destination = destination.position;
}
if (timeLeft <= 5.0f && Time.time <=1.0f) {
// spawn
GameObject g = (GameObject)Instantiate(teddy2, transform.position, Quaternion.identity);
// get access to the navmesh agent component
NavMeshAgent n = g.GetComponent<NavMeshAgent>();
n.destination = destination.position;
Use a timer of this sort.
float spawnTime = 1; //1 second float timer;
void Update()
{
timer += time.deltaTime;
if(timer > spawnTime)
{
//spawn something
timer = 0; //reset timer;
}
}
hi yea on the beginning i was using timer or timeLeft, but if im using this method i will not be able to spawn another type of creep at time 5.0f
Answer by robertbu · Nov 15, 2013 at 05:34 PM
For this kind of thing, a timestamp is a way to approach the probelm. Rather than look at elapsed time, look at the future time you want something to be done:
timestamp1 = Time.time + 1.0f;
Then you just check the timestamp against the time:
if (Time.time >= timestamp1) {
// Do something
timestamp1 = Time.time + 1.0f;
}
If you have multiple events, then use multiple timestamps.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to Subtract the score 1 Answer
Unity how to make different enemy spawn one at a time 1 Answer
How to deactivate or pause prefab array when pause button is pressed? 1 Answer