Instantiate on a path

What will be the best way to instantiate objects on a path, but at random points? I’m trying to instantiate obstacles on a road. The obstacles are supposed to clone at random points, but on the road.

im almost helpless and cant think of a good way to do this. (there are around 5 types of obstacles on 5 different tracks)

Hi,

You could make some empty objects which would function as a spawn point.
And then you could make a List of all those spawn points and let a script select a random spawn point.

the script on the spawnpoint could look like this:

// All the possible obstacles
var myObstacles : List.<Transform> = new List.<Transform>();
// The range where the obstacles are spawned in
var range : float;

function SpawnObstacle(){
var myLoc : Vector3;

// set a random position around the spawnpoint
myLoc = Vector3( Random.Range( transform.position.x - range, transform.position.x + range), transform.position.y + 100 , Random.Range( transform.position.z - range, transform.position.z + range) );

// Instantiate a random obstacle at our random position
Instantiate( myObstacles[Random.Range(0, myObstacles.Count)], myLoc, Quaternion.identity );

}

The obstacle will then be spawned very high so that it can raycast down to adjust it’s height to the ground, that would be something for you to script :wink:
if the track is flat just remove the “+ 100” from the Y location.

Hope this helped :slight_smile:
NOTE: I haven’t tested any of this, so it may contain some errors :slight_smile: