- Home /
Objects following a path freeze/lock when hitting each other
I've made a basic traffic system in my app. It involves my vehicles following a simple path, and that's all they do - follow the path. When they hit any certain node, they get shot off onto another path, and that's that. Same applies for spawning and destroying, etc. They don't use physics at all; they're kinematic, path following bots.
The problem I'm facing now is how the cars collide with each other. Each vehicle contains a solid mesh collider ("solidLayer") and in front of each car is a capsule trigger collider ("triggerLayer"). These colliders are in different layers, and ignore any other collision in the game. The vehicle movement script defaults to GO movement, and if OnTriggerStay() is called, the vehicle slows down (brakes).
Everything works fine, but sometimes a vehicle doesn't slow down in time, hitting the car in-front of it, causing them to partially overlap with each other. When this happens, OnTriggerStay() is sometimes called for both vehicles. They're both permanently braking, causing the traffic system to fail (cars behind just queue up in line).
Is there any way I could cheat the system and prevent this from happening, or at least how to elegantly handle this? There's many things I could do - for instance, I could just toss out both cars and return them to the object pool from which they came. However, this would look funky (disappearing cars!) and doesn't look professional. Another way could be to reduce the amount of cars on the road and increase the duration of the spawn points, but that's just trying to make the situation occur less often then dealing with it correctly.
I like it. It sounds realistic. An accident causing a traffic jam?
Answer by LyanApps · Apr 15, 2013 at 02:19 PM
You could make both cars aware of each other and use some sort of random time logic.
Wait Random.Range(0.0f, 3.0f)
If (time elapsed and other_car not going){
self going = true
Continue driving logic
else wait another Random.Range
You could also give each car a priority upon spawn
car = Instantiate(car)
car.setPriority = num_cars++;
Then the higher priority car always goes first.
I didn't accept this answer because it's not what I did, but it put me on the right path! The vehicles are now aware of each other by sorts, and that they make sure there's enough distance between the two (the vehicles now contain a radius property).
If the target movement distance between the two cars is less than the sum of the radius, the second car doesn't move to that spot! I simply forgot to add a basic logic testing. I assumed the position was always valid to move to. Now I just test it to make sure! Thank you!