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.
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.
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
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…