Hi, sorry if my question seems rather simple. I’m completely new to C# and Unity.
I’m making a simple “Cube-Field” style game. I’m procedurally generating my obstacles like so:
void SpawnObstacles () {
int randomIndex = Random.Range(0, spawnPoints.Length);
for (int i = 0; i < spawnPoints.Length; i++)
{
if (randomIndex != i)
{
Instantiate(obstacle, spawnPoints[i].position, Quaternion.identity);
}
}
}
This is almost what i want - my spawnpoints follow the player as they move and spawn obstacles infront of the player - however instead of the obstacles all being in a line, i want the Z position to be random for each obstacle. How do i go about doing this?
If the objects are going berzerk, they probably have a Rigidbody and Collider attached. Is that what you want? Maybe you just need to lift them above the ground a bit?
ALSO, hold up your left hand to your left side, with your left index finger up, your left thumb pointing at your head: that is the Unity3D coordinate system. The thumb is +x, the index is +y and the palm faces +z. Is the Z axis really the one you want randomized for a player? Just checking… it might be, but if the player is running around on the ground, all he would have to do is run to less than Z = 0 or greater than Z=5 and be clear of your obstacles, as you are discarding the original Z component when randomizing.
Another way to randomize relative to a point is this:
Vector3 myOriginalPoint = <however you get your point>;
myOriginalPoint += Vector3.forward * Random.Range( 0.0f, 5.0f);
That will take the original Vector3 and add a random distance from 0 to 5 in the +Z direction ( “down camera” for the default Unity scene setup)
1 Like
Yes, the Z-Axis is definitely what im going for in this example. The player himself isnt randomized, they are pushed along the Z axis with randomised obstacles being infront of them.
I ended up solving my predicament shortly before you replied with:
Vector3 spawn = new Vector3(spawnPoints[i].position.x, spawnPoints[i].position.y, (spawnPoints[i].position.z + Random.Range(0f,5f)));
Instantiate(obstacle, spawn, Quaternion.identity);
1 Like