- Home /
2D Infinite Runner - prevent obstacle from spawning over gap
Hey there,
I'm dealing with a new issue and I couldn't find a good solution to it by now, so I hope someone of you might help me.
The thing is, in my 2D infinite Runner, I got obstacles spawning randomly on the way, but I don't want them to spawn to close or even inside a gap. My first attempt was to create empty gameobjects with a collider to mark gap areas in which the obstacles shouldn't spawn. So if an obstacles spawns inside such an area, it would be moved to the right until it left the area. (I achieved this with an Physics.Overlap function which checks the collision with a certain layer and controlls a bool. As long as the bool is set to true, the obstacle is moved). The problem with this is, that it might happen that if two gaps are following up, the obstacle will be stuck inside the second gap anyways because it cannot move any further (the obstacles spawn above the floor and fall down, so when the overlap bool is true they're pushed to the side as long as they do not touch the ground). I thought about simply destorying the obstacle if it spawns inside a gap, but I couldn't figure out how to to that to only this one specific obstacle...if I tell the script to destroy the gameobject, all obstacles are completely destroyed...(they all have the same script attached to check the gap overlap).
Would be great if anyone could help me out.
Thank you in advance!
Answer by Teku-Studios · Apr 29, 2015 at 09:10 AM
Without looking at your code is hard to say. If you have already spawned a gap, why should an obstacle spawn at that same position? You might save all the positions with hazards (whether it is a gap or an obstacle) and never spawn anything in that position again.
if you have a list or an array of hazards, you could tell your code to always spawn a new hazard in the next position. So let's say that you have a size 6 array of fixed positions with spawned objects linked to them like this
[gap, obstacle, gap, gap, null, null]
Your next hazard, gap or box, should spawn at the next position, preventing it from being placed over an already existing hazard. This is a very rough idea, I'm afraid I can't help further without deeper knowledge of your code setup.
Since it is an endless runner kind of game, you could clear the hazards array/list once it's full so you don't have a potentially infinite variable that could destroy your memory in the long run. But again, this is just the first solution I've come up with, and I assume it's far from ideal.
Well I creaeted the game based on this tutorial:
http://www.raywenderlich.com/69392/make-game-like-jetpack-joyride-unity-2d-part-1
So basically I have the floors generated and then I have the obstacles generated in two different lists. The floors are whole sets of different smaller pieces (e.g. straight, gap, transition to higher level, high straight, gap, transition to normal level...) The obstacles spawn on a higher position and fall on to the ground. That's why they can spawn above a gap.
Answer by N00MKRAD · Sep 08, 2017 at 06:19 AM
Just let the obstacle check if there's a ground (with raycast or something else) on Start() and Destroy it if not.
Your answer
![](https://koobas.hobune.stream/wayback/20220613194326im_/https://answers.unity.com/themes/thub/images/avi.jpg)