I need a script were the enemy spawns every 2 minutes how do I do this
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)
}
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
; )