- Home /
random enemy
I want to do a 2D plane on the mobile phone game
I have three enemies Let them randomly generated But also an increasing rate (the enemy is automatic parallel move), an increasing number of
May I ask how to write code?
Answer by G_Sacristan · May 31, 2011 at 08:29 AM
var enemies: Transform[]; //array that holds all enemies, just set size to 3 and drag them in inspector as prefabs
var lastSpawn: float;//holds the time when enemy was last time instatiated
var spawnPos: Vector3;
var spawnTime: float=3.0;//seconds for example
functon Start()
{
lastSpawn=Time.timeSinceLevelLoad;
spawnPos=transform.position;//to increase performance a bit
}
function Update()
{
if(Time.timeSinceLevelLoad-lastSpawn>spawnTime-(Time.timeSinceLevelLoad/5))
{
SpawnEnemy();
}
}
function SpawnEnemy()
{
var idx:int=UnityEngine.Random.Range(0,2);
Instantiate(enemies[idx],spawnPos,Quaternion.identity);
lastSpawn=Time.timeSinceLevelLoad;
}
Cheers!
Answer by shelin · Jun 01, 2011 at 05:21 AM
Error
IndexOutOfRangeException: Array index is out of range. NewBehaviourScript.SpawnEnemy () (at Assets/js/NewBehaviourScript.js:25) NewBehaviourScript.Update () (at Assets/js/NewBehaviourScript.js:18)
What happen?
My window 240*360.
Answer by shelin · Jun 01, 2011 at 05:47 AM
My enemies are randomly generated from both sides
Then there are three kinds of enemies appear to be random
The level of the enemy is moving in the direction from right to left or from left to right
The enemy faster and faster
The number of the enemy will more and more
May I ask how to write code?
Your answer
