I am trying to make a endless runner with 4 lanes that the player can move in. The problem I am having is I want to randomly spawn enemies within the different lanes, but make sure that all 4 lanes are never blocked at the same time. Here is a demonstration of what I want what is happening.
—E—————E—————E——
P————E———————E——
—E———————E———E——
——————E——————E——
I want the enemy to spawn/instantiate randomly on the x axis within each lane as you can see by the scattered “E”'s, but then what eventually happens as you can see on the very right all the enemies will eventually spawn inline and not allowing the player to pass.
Each enemy I have has the same script attached script attached and here is what I’m using at the moment:
float delay = Random.Range(20f, 50f);
Vector3 NewPos = new Vector3 (MyTransform.position.x, MyTransform.position.y + delay, MyTransform.position.z);
Transform Enemy = Instantiate (MyTransform, NewPos, MyTransform.rotation) as Transform;
I was wondering if it would be better to start fresh and have just one script on maybe an empty game object that instantiates a prefab of enemy randomly on the lanes and checks that they are not in the same place. Or if it’s possible to somehow use my current script and check if the enemy spawns across all 4 lanes within 2 pixels (just an example) of each other then either destroy or move one of them at random.
I have searched a lot trying to find a solution to this problem and haven’t found anything yet, I don’t think the solution will be too complicated, I’m just not too sure where to start.
Thanks