- Home /
how to get a enemy to spawn
I need a script were the enemy spawns every 2 minutes how do I do this
Answer by Maarten · Dec 11, 2010 at 09:36 PM
var startTime : float; var enemy : Transform; //Your enemy prefab
function Update() { if (startTime == 0) //New time { startTime = Time.time; //Set the current time } else { if ((Time.time - startTime) == 120) //Check if 120 seconds have passed { startTime = 0; //Set the starttime back to 0 Instantiate (enemy, Vector3(0,0,0), Quaternion.identity); //Create an enemy (transform, position, rotation) } } }
Or using the following method:
function Awake() { InvokeRepeating("spawnEnemy",0,120); }
function spawnEnemy { Instantiate (enemy, Vector3(0,0,0), Quaternion.identity); //Create an enemy (transform, position, rotation) }
The top bit of script is working perfectly! Although when the clone spawns it is indeed spawning without a target in the AI settings. How can I fix this?
Answer by BinarySpace · Nov 16, 2012 at 04:35 AM
Thanks for the great code... however there is a small bug. The line
if ((Time.time - startTime) == 120) //Check if 120 seconds have passed
should be changed to: if ((Time.time - startTime) >= 120) //Check if 120 seconds have passed
This is because the test will usually fail since the difference is unlikely to ever be EXACTLY 120
; )
Your answer
Follow this Question
Related Questions
How to make UNITY multiplayer with two HTC VIVE in the same real room? 0 Answers
random respawn and respawn delay 1 Answer
How to make a spawner? 1 Answer
How to respawn items along with player 1 Answer
Spawn and Destroy problem snake like 2 Answers