Hello guys,
In my project i have a low fps when I spawn a lot of cars. Every car contains a PrioritySensors() method witch has many raycast used for sensors.
What can I do for a better performance?
Should I call the method after few seconds?
I had a similar problem when making traffic vehicles with raycasts. The solution I found that worked best, was to make it so the raycasts are not called every frame, but instead skip like 3-5 frames before casting again, which you may tweak differently for your game. And another thing that helped a lot, was to make sure they didn’t all raycast at once, and instead stagger those raycasts out so maybe 1/3rd does a raycast, then a frame or two later, another 1/3rd of them do a raycast, and so on.
You may create a raycast manager and make sensors ask that manager providing a ready callback instead of doing a raycat directly. It that manager you can queue requests and effectively limit number of raycasts per frame to meet your performance requirements. Just make sure not to order another raycast while previous isn’t completed for every single sensor to avoid limitless queue growth.
The raycasts are not the majority of what’s going on - as you can see, FixedUpdate is about 10ms, while the raycasts are only 1.2ms.
You should deep profile to see what’s actually going on. That being said, the other advice in this thread is solid - spreading work over frames is often useful.
Nice, a lot of problems
Should I put conditions in Update method for calling another method, or is ok to put condition in my method and return when condition is false ?
this is my Update method:
private void FixedUpdate()
{
if (_spawned == true)
SpeedBooster();
else
MotorTorque = _oldMotorTorque;
ApplySteer();
LerpSteerAngle();
GetAngle();
SetDirection();
Drive();
IsTooFast();
Braking();
Accelerate();
SpeedoMeter();
PrioritySensors();
//AvoidingSensors();
//GameObject sun = GameObject.Find("Sun");
//ToggleFrontLights(sun.GetComponent<sun>().Night);
if (ChangeLane && _distance < 30)
StartCoroutine(ChangeLaneLights());
SetTrafficDistance();
}