- Home /
How to implement traffic system efficiently in Unity game to run smoothly on mobile device?
Recently developing a racing game, game is fine but when i added traffic system (AI cars with waypoint following) the game becomes laggy ans slow. I used almost 300+ more cars with rigidbody and box collider attached. I searched about solution of the problem : Some said it's the amount of rigidbnody or collider for making the game slow. But if I remove colliders then I won't be able to detect the collision between my main player's car and AI traffic cars. Any solution or suggestion ?
You can also try new unity entity component system, this can help you to spread all your traffic cars behaviours in background threads. Another think to mention is that why should your traffic system control all 300 cars using physics, you may optimize it a lot if turning on physics for cars only in proximity to the player(s) and all other distant cars can be handled by simple translations and rotations, also you should definitely use LOD for traffic car 3d models.
Answer by Turzoxpress · Aug 29, 2016 at 01:44 PM
Problem Solved!
I used Unity Profiler to check the main cause of slowing the game. I found that I have a common script for the 300 traffic cars to follow a certain waypoints. That scripts updated every frame, that's why the game became slow.
Solution :
I moved the waypoint follower code from Update function to LateUpdate, for this, the game doesn't get hanged while starting the game cause LateUpdate always calls after all Update functions have been called.
Then I measured the distance between my Player car and each traffic cars, I take a minimum distance (in my case : 130m ) between Player car and a traffic car so that which traffic cars are away from this certain distance are inactive. I mean I make them active when they are in 130m range and make them inactive while they are not in that range.
And now my game becomes way faster!
i am new in untiy and i have no idea of distance findin you do in your game . can you plz send me the following task code Turzoxpress Then I measured the distance between my Player car and each traffic cars, I take a $$anonymous$$imum distance (in my case : 130m ) between Player car and a traffic car so that which traffic cars are away from this certain distance are inactive. I mean I make them active when they are in 130m range and make them inactive while they are not in that range.
And now my game becomes way faster!
Your answer
Follow this Question
Related Questions
Make a Lapping System for a Car Game 1 Answer
AI car Script problem 1 Answer
AI Car Problem 1 Answer
Ai turning problem on Android - Question solved 0 Answers
How To Add AI To Car/Racer? 4 Answers