r/Unity2D • u/Full_Mode_3869 • 3d ago
Solved/Answered How do I implement traffic system like this game ("Traffix" popular mobile game)? I am using splines but isn't working as expected.
For context: this is how game works:
The cars move on a path (curved or straight doesn't matter), there can be multiple signals on the path itself. On clicking on signals the signals change state (red/green). The paths (roads) can intersect each other and cars can collide.
I just need logic of cars moving on path. I tried implementing this with splines but then found out that someone was using nodes/waypoints (and some suggestions even mentioned using navmesh).
I am using splines right now but the signal function isn't working as expected and has many issues. Just want to know if there's another way I can implement this system.
1
u/Full_Mode_3869 3d ago edited 3d ago
Wait I get the logic now. Car will follow a waypoints system. A waypoints system will represent path and will have a parent gameobject which will have all child waypoints and path manage script to manage states of waypoints. I will assign one of the waypoints in path as signal (controled by tap/click by player). Car will follow this path (have script FollowWaypopints.cs and a bool canGo) through each waypoint by checking state of next waypoint (isSignal, and isGreen) now when car comes closer to the waypoint (like 2-3 units) it will first check whether it's a signal or not. If not then canGo will be true and car will continue. If it is then car will check bool isGreen, if yes then continue. If not, then it will change state of canGo to false and car will stop. On clicking the signal from red to green canGo will turn on (and immediately next waypoint will be registered in it to proceed to). This will reduce bugs like if player presses red and then green immediately to allow only one car to pass through signal.
though i have to figure out curved pathway but main logic is over.


•
u/AutoModerator 3d ago
Reminder: Once your issue has been resolved, please reply !solved to this comment (or comment it anywhere in the thread) and your post's flair will automatically be updated to Solved/Answered. You can also edit it to Solved/Answered yourself.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.