- Home /
Randomly Generated Objects inside an area
I need a code to spawn in multiple objects in specified area when the scene is first loaded. I'm trying to make a island resource survival game kinda like the game "Stranded" or "Stranded 2", And I need to spawn in things like trees/rocks in a certain part of the island, but still at random spots with a random number of things. If you do submit a script, I would like you to please explain what everything means and what everything does so I can change things if need be, If that wouldn't be that hard for you guys? I haven't messed with things like this before, and I want to learn what I'm doing. any help would be much appreciated.
What is the area like you want to spawn things in? is it round or squared? and do you want the things on a grid like layout or totally random?
Answer by erick_weil · Feb 18, 2014 at 12:22 AM
try by creating a function and calling then in start()
make a area Vector3 paramater and a GameObject for the Instance.
function generateisland(NumObjects : int,wichobject : GameObject,area : Vector3)
{
.
.
.
iniciate using a loop to create as many objects that you want of that type.
for(var i=0;i<NumObjects;i++)
{
.
.
.
now make a Random posi to Instantiate this.
posi = Vector2(Random.value*area.x,Random.value*area.z) ;
and the y position find by a raycast from a very high point,to not raycast down of the terrain, like this:
var hit : RaycastHit;
var yofsset : float;
if (Physics.Raycast (Vector3(posi.x,500,posi.y), -Vector3.up, hit)) {
yofsset = hit.distance;
}
and the y value of the objects position is seted by 500 - yofsset...
var realposi : Vector3 = Vector3(posi.x,500-yofsset,posi.y);
Instantiate(wichobject,realposi,wichobject.transform.rotation);
now you have a random instanciator! note that can have merging objects, this is only a test.
What would all the code look like put together? I'm not sure what order to type all o this.
#Pragma Strict
var object : GameObject;
function Start()
{
// the 100x100 is the area that become filled with your objects begining at origin
generateisland(100,object,Vector3(100,0,100));
}
function generateisland(NumObjects : int,wichobject : GameObject,area : Vector3)
{
for(var i=0;i<NumObjects;i++)
{
posi = Vector2(Random.value*area.x,Random.value*area.z) ;
var hit : RaycastHit;
var yofsset : float;
if (Physics.Raycast (Vector3(posi.x,500,posi.y), -Vector3.up, hit)) {
yofsset = hit.distance;
}
var realposi : Vector3 = Vector3(posi.x,500-yofsset,posi.y);
Instantiate(wichobject,realposi,wichobject.transform.rotation);
}
}
Am I to attach this script to an empty game object, or?
attach this to any gameObject, and before asking here, test by yourself, the unity will not uninstall if you made ones mistakes...
and you need to assign a object to instance, in the inspector, see the "object" variable
an image to you
see the highlight area, is there you need to click to select a prefab to random instantiate, and re$$anonymous$$d to add your terrain(or a plane) to your scene, filling the area, (default 0,0 - 100,100)
I tested it and got errors when I used the empty game object, So I was trying to make sure that wasn't the problem...
Answer by thornekey · Feb 18, 2014 at 01:43 AM
when i was trying to spawn a pillar at a certain height i did this:
public double MaxHeight = 3.4f;
public double MinHeight = -6.4f;
void Spawn(){
float rightScreenBound = 18;
float randomY = Random.Range(MinHeight, MaxHeight);
Instantiate(PillarObject, new Vector3(rightScreenBound, randomY, 0), Quaternion.identity);
}
the variables up the top are positions :)
hope its relevant
Answer by Nidre · Feb 18, 2014 at 01:36 PM
You can take a look the script a have wrote a while ago. It creates objects in a defined area. You can define density, shape etc..
Your answer
Follow this Question
Related Questions
Why my area object is bigger than my object ? 1 Answer
Help with Generating Random Tiles 1 Answer
Random Button / Plane Generation. 0 Answers