- Home /
Endless/infinite runner random enemy spawning multi lane
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
Interesting problem. Can different obstacles move at different speeds? If not, you could simply never spawn more in a given interval than your total number of lanes, right?
Answer by AlwaysSunny · Nov 01, 2014 at 06:56 AM
Oh, I re-read this and see what you're doing here. This situation calls for a manager script. It's a powerful concept you should master early. Your EnemyManager script should determine when, which, how many, where, etc - your enemies spawn.
It can also hold a collection of them, help with creation / destruction, and even help decide what spawn locations are acceptable based on the current status of the game.