One of the tedious things that I’m doing over and over in my game is randomly placing the objects that my player can pick up.
Now I know I can write something like this in Maya, but it will not get any of the prefab info that I put into it, nor will it be easy to change values once placed in Unity, so I have opted for placing in Unity, rather than through a routine in Maya.
I’m wondering if there is a way to do this, a script that I can specify across my game that I can randomly place a hundred or so objects instantly so that I can devote more time to the creative aspects rather than the tedious.
Honestly, I’m not even sure where to start creating a tool like this in Unity.
thanks for any thoughts!
static var TOT = 0;
var object : Transform;
var MaxObjects = 10;
var MinTime = .1;
var MaxTime = .8;
var MinX = 0;
var MaxX = 39;
var MinY = 0;
var MaxY = 14;
function Start ()
{
while (true) {
if (TOT < MaxObjects) {
yield WaitForSeconds(Random.Range(MinTime, MaxTime));
var randomPos = Vector3 (Random.Range(MinX, MaxX), Random.Range(MinY, MaxY), 0);
Instantiate(object, randomPos, Quaternion.identity);
TOT++;
}
yield;
}
}
This is a copy / paste of some code in my game that spawns objects randomly over time until there is 10 MaxObjects. You could modify it to instantly spawn however many objects. Make sure you constrain the x,y,z coordinates accordingly so that your objects only spawn randomly where you want them to.
This was really helpful, working on refining the script for my needs. I’m thinking to make everything a rigidbody and drop them into the scene so they sit on the ground instead of floating, and having different activation spawn points, so that there is never more created than I need.
thanks