Hey everybody! I’ll cut to the chase. I’m trying to make what I call “Perfect traffic”. It’s a simulation of 4 streets with 4 different cars that can only spawn if a series of conditions are met. The result (at least what I wanted) was random cars spawning and miraculously not colliding. Here’s the scene:
I actually got them to not collide, but the result was that there was a lot of empty space. But the real issue is that, while looking into this, I discovered that they always spawn in a certain order (not even the cronological order!). Car2, Car, Car4, Car3. I mean, the RandomInRange variable resets until the next number fits the pattern!
I think the problem might be in ‘the conditions’ part of the code. Simplified, to check if a certain car can spawn, my code: (This is clarification for the next images, I’d skip this part if you’re reading the post, it’s a hard read)
- Picks the “main” car that’s closest to it’s spawn point and, at the same time, closest to the car that will be spawned (If you think about it, every car has one “main” car, the one that has more chances to collide with, the one on the closest street that goes in the other direction).
- Checks if that car is in it’s “danger zone” (for example, in my game, if we want to spawn Car and the position Y of Car2 is more than 518, then it will surely collide with it). If it’s in it’s danger zone, then it doesn’t spawn and resets the timer.
- If it’s not in the danger zone, then it also checks for the next threat, the other opposite-direction car.
If there’s none or it’s not in it’s danger zone, then the car spawns. If it is on the danger zone, then the timer is restarted.
Of course I don’t actually expect help from just these steps, they serve to explain how the code works and what I wanted to do (If there’s a better option I’ll change it in a heartbeat).
Those are the directions of the cars
At the beginning of the scene, and timer and RandInRang event (The “firstTime” boolean works backwards, it’s supposed to indicate that this is not the first round. I needed separate events because in the first round the game doesn’t need to check for existing cars because, well, there’s none)
This is the first “real” car event. Its structure is repeated 3 other times for the other cars, which I felt was unnecesary to display. Basically the same with different cars and positions.
This one is just to show the bottom event. You can place the 3 pictures together and you will see its the whole code.
That’s all I thought of including. If there’s anything else I should add for clarification please let me know and if you have a solution or an improvement (even if it’s changing everything), I’ll be very available these days. Thanks a lot for reading!