- Home /
How to make enemies spawn in random y position?
Hi I have a box as the child of my player and with this code it spawns enemy units every 3 seconds. What I need is for the units to spawn in a range of random y positions in side the box (the box is very tall to accommodate a wide range of positions). If not this then I can make the box smaller or switch it for an empty gameobject and have it move around in the y axis to spawn the units from there. I've read up on random.range but I am unsure on how to utilize it here. This script is attached to the spawn box. I also need advice as to what I can use to make my flying monsters fly to my players current position at the moment the spawn, but not in a homing missile type of way(dont need this scripted for me I would just appreciate some advice o what function I could use). Thank you in advance for any response.
var hero : GameObject;
var flying : GameObject;
var spawner : Transform;
InvokeRepeating("Spawn", 2, 3);
function Update ()
{
var positiony = transform.position.y;
positiony = hero.transform.position.y;
}
function Spawn()
{
Instantiate(flying,spawner.position, spawner.rotation);// its instattiating at 0,0,0 i need to follow this up with corrdinates
}
Answer by robertbu · Jun 15, 2013 at 05:45 AM
If I understand what you are doing correctly, you can use Random.Range() like this:
function Spawn()
{
var v3 = spawner.position;
v3.y += Random.Range(-2.5, 2.5);
Instantiate(flying,v3, spawner.rotation);
}
I don't know how big your box is. You need to tune the -2.5, 2.5 to the size of your box.
thanks it worked but even better I learned how to use this function.