Creating Random Spawning
Here is an example of how to create a spawn of an object in 3 potential positions. In my scene I have set up 3 differing locations using empty objects which I then apply to the transform variables in the script. The only other thing you’ll need is a prefab!
-
var timer : float = 0.0;
-
var spawning : boolean = false;
-
var prefab : Rigidbody;
-
var spawn1 : Transform;
-
var spawn2 : Transform;
-
var spawn3 : Transform;
-
-
function Update () {
-
//check if spawning at the moment, if not add to timer
-
if(!spawning){
-
timer += Time.deltaTime;
-
}
-
//when timer reaches 2 seconds, call Spawn function
-
if(timer >= 2){
-
Spawn();
-
}
-
}
-
-
function Spawn(){
-
//set spawning to true, to stop timer counting in the Update function
-
spawning = true;
-
//reset the timer to 0 so process can start over
-
timer = 0;
-
-
//select a random number, inside a maths function absolute command to ensure it is a whole number
-
var randomPick : int = Mathf.Abs(Random.Range(1,4));
-
-
//create a location 'Transform' type variable to store one of 3 possible locations declared at top of script
-
var location : Transform;
-
-
//check what randomPick is, and select one of the 3 locations, based on that number
-
if(randomPick == 1){
-
location = spawn1;
-
Debug.Log("Chose pos 1");
-
}
-
else if(randomPick == 2){
-
location = spawn2;
-
Debug.Log("Chose pos 2");
-
}
-
else if(randomPick == 3){
-
location = spawn3;
-
Debug.Log("Chose pos 3");
-
}
-
-
//create the object at point of the location variable
-
var thingToMake : Rigidbody = Instantiate(prefab, location.position, location.rotation);
-
thingToMake.AddForce(Vector3(0,0,100));
-
-
//halt script for 1 second before returning to the start of the process
-
yield WaitForSeconds(1);
-
//set spawning back to false so timer may start again
-
spawning = false;
-
}









Just a few things :
* Mathf.Abs returns the absolute value. Mathf.Abs(3.4) is 3.4. What you are looking for is Mathf.RoundToInt
* I’m not 100% sure because it’s JS and I’m more a c# guy, but that’s pointless anyway because you passed two int, so it Random.Range will naturaly returns an int.
* I certainly hope that JS makes that difference, because Random.Range(1.0f, 4.0f) might returns you 4.0f which is not handled in your code.
* On a personnal preference, I would rather do an array of Transform (maybe named SpawnPoints) which would gives you more flexibility and less code
What’s the problem with this?
Mathf = Math FLOOR, so the argument will always be rounded down.
By generating a random floating point number between 1 and 4, will give a result within the range of 1.001 – 3.999.
This number then effectively ditches the decimal to become an integer, and will only ever produce the integer 1, 2 or 3 as a result.
As for the usage of an array. You’re totally right, but I think in the interest of tuition, keeping it simple is probably the way to go.
Just my 2 cents.
Keep up the good work bud! =)
Jonny , you’re totally right, and this is a bit of an oversight on my part so apologies to anyone I’ve misled here! thanks for taking time to point this stuff out.
Thank you very much dude am new to scripting. was dying to instantiate an character if the score is 20 and it keeps increasing but now i know where i was wrong 10 stars for this