- Home /
Objects appear randomly in specific length and height
My character will be blast off from a catapult. It will bounce until it stops (speed = 0). In the meanwhile there will be objects in the sky or ground that if the character hit them they will increase it's speed or slow it. My question is, how can i make this objects appear randomly at all length that is e.g 50,000 meters(and height for the sky objects e.g 1,000 meters)? And what troubles me is that in level 1, one of the objects number of appearance will be 40% and on level 2 will be 60%. How do i do that? I am new in unity and i'm confused with the scripting part. Thank you in advance!
Answer by robertbu · Mar 06, 2013 at 08:36 AM
This code creates GameObject primitives at random locations around the origin bounding by minimum/maximum X, Y, & Z values. Attach it to an empty game object:
var minX = -10.0;
var maxX = 10.0;
var minZ = 5.0f;
var maxZ = 35.0f;
var minY = -2.0f;
var maxY = 7.0f;
function Start () {
for (var i = 0; i < 300; i++) {
var go = GameObject.CreatePrimitive(PrimitiveType.Cube);
go.transform.position = Vector3(Random.Range(minX, maxX), Random.Range(minY, maxY), Random.Range(minZ,maxZ));
}
}
Upvoted =) @robertbu has suggested a good way....by using Primitive you can have many objects in optimum draw calls and by the Random.range you can place the objects randomly......enjoy