I’m trying to make a side-scrolling 2.5d game in which sections of the levels are prefabs, and are triggered just before the player reaches them (so they appear offscreen before you get there). So far the crude manner in which i have got something similar to work is by having an invisible collider/trigger object which the player’s craft hits, when it hits it the game instantiates a prefab level section ahead of the player.
It sort of works, but it is unreliable: the location of the prefab level segment seems to vary with respect to the x axis (the direction the player craft is moving in), and sometimes multiple level segments get instantiated when only 1 should be expected.
So my question is, how would you approach this in a more efficient manner? The idea is to eventually be able to have the level segments selected at random from a selection of different ones, to create an infinite level which isn’t preprogrammed (sort of like templerun). Obviously i ought to be destroying old segments too once they are offscreen.
// instantiates a new level segment on collision. doesn't seem to work that well though, so different means to do this is required.
void OnTriggerEnter(Collider other){
if(other.gameObject.tag == "Player"){
Debug.Log("Trigger Next Level Segment");
Rigidbody newLevelSegment = Instantiate(levelsegment, transform.position = new Vector3((transform.position.x + xOffset),yOffset,zOffset), transform.rotation) as Rigidbody;
}
}