- Home /
Fixed Spawn Amount
Ok, so i've got a script which is applied to different object which tells it to spawn cubes when a particle hits the object.
So I've got this script:
var ThePlace : GameObject;
var hasportal : boolean;
var portalblue : GameObject [];
function newportal () {
if (hasportal==false){
theportal=Instantiate(portalblue[(Random.Range(0, portalblue.Length))],ThePlace.transform.position,transform.rotation);
theportal.transform.eulerAngles.x=-90;
theportal.transform.position.x=ThePlace.transform.position.x+1;
}
}
I want to set it up so that every one in a thousand cubes which spawn, a small cube spawns. (portalblue is the cube, The Place is the place where they spawn, hasportal is if the particle hits the object)
I don't want to go into each object in the Inspector and add 1 thousand elements, 999 being a cube and 1 being a small cube.
How do I set it up in this script so that I can set percentages? Thanks
Answer by robertbu · Feb 17, 2013 at 05:40 AM
You can do it through absolute count, or you can do it through random number. For count you could have a variable that you increment each time you spawn. And then code something like:
if (iCount >= 1000)
{
iCount = 0;
// Do the small cube spawn.
}
For random you could do something like:
i = Random.Range(0,1000);
if (i == 0)
// Do the small cube spawn.
The second method produce small cubes on average every 1000 spawns.
I'm confused about your answer. I assumed you want to Instantiate something other than...
theportal=Instantiate(portalblue[(Random.Range(boxspawn, portalblue.Length))],ThePlace.transform.position,transform.rotation);
...every 1000 uses. You would have a variable iCount which you declare at the top of the file and increment every time you do an Instantiate. You code might look something line:
if (iCount < 1000)
{
theportal=Instantiate(portalblue[(Random.Range(boxspawn, portalblue.Length))],ThePlace.transform.position,transform.rotation);
iCount++;
}
else
{
// Instantiate the small box;
iCount = 0;
}
I may be missing what your are trying to accomplish here. If do add more details and I'll try again.
This script:
var ThePlace : GameObject; var hasportal : boolean; var portalblue : GameObject [];
function newportal () {
if (hasportal==false){
theportal=Instantiate(portalblue[(Random.Range(0, portalblue.Length))],ThePlace.transform.position,transform.rotation);
theportal.transform.eulerAngles.x=-90;
theportal.transform.position.x=ThePlace.transform.position.x+1;
//theportal.transform.eulerAngles.y=270;
}
}
The Random.Range needs to be edited so that 1 in a thousand boxes will spawn the small boxes. How will I accomplish this and it has to be in the if (hasportal==false){ .
Any help will be greatly appreciated. I am slowly creating a game.
I changed the 'small box' into a faulty cube: http://www.youtube.com/watch?v=NbGcIm27Twg As you can see I set a wall to spawn only faulty cubes just to show you what they are like. The Blue boxes you soon see in the video are the main boxes. You could as well say that the faulty cube is more or less an easter egg (Term for a hidden secret) because the gun is textured quite dusty.
Not what I was looking for. I'm trying to understand what the line of code looks like for Instantiating a small box. Anyway putting together the code you posted with what I posted would look like:
if (hasportal==false){
if (iCount < 1000) {
theportal=Instantiate(portalblue[(Random.Range(boxspawn, portalblue.Length))],ThePlace.transform.position,transform.rotation);
iCount++;
theportal=Instantiate(portalblue[(Random.Range(0, portalblue.Length))],ThePlace.transform.position,transform.rotation);
theportal.transform.eulerAngles.x=-90;
theportal.transform.position.x=ThePlace.transform.position.x+1;
}
else
{
// Instantiate code for the small box goes here
iCount = 0;
}
}