- Home /
Reducing overhead while controlling many units
Hello,
I'm making an RTS game that aims to have a LOT of units on the screen at any given time.
Right now, in the early stages, I'm getting heavy FPS drops when commanding 100-200 units on screen.
Using the profiler, I checked where my lag is coming from, and it's coming from the update function of each individual unit. The update function of each unit is as follows:
void Update () {
if (isSelected && CameraControl.commandIssued && completedCommand && !isActive)
{
computeDestination(CameraControl.destination);
isActive = true;
}
else if (isSelected && isActive & !completedCommand && CameraControl.commandIssued )
{
computeDestination(CameraControl.destination);
}
else if (isActive && !completedCommand)
{
moveToLocation(globalMoveDirection, CameraControl.destination);
}
else
{
//idle
}
}
Here is that function being called each frame:
public void moveToLocation(Vector3 movDir, Vector3 destination)
{
unitCharCont.Move(movDir * Time.deltaTime * unitSpeed);
distanceFromObjective = Vector3.Magnitude(transform.position - destination);
if(distanceFromObjective < 0.5f)
{
completedCommand = true;
positionComputed = false;
isActive = false;
}
}
Broken down, the code basically says "when a command is issued, compute my destination a single time, then update my position using CharacterController.Move()
every frame until I reach my destination.
Is there a more efficient way to move units? I.e. does CharacterController use more resources than a RigidBody? Or, does anybody have any ideas as to how I can cut down these 200+ calls per frame to 1 call, and control the selected units as a single pack? I'm sure I could get creative, but I'm reaching out in case anybody has encountered this and has a good solution.
Thanks for any help! Vranvs
Your answer
Follow this Question
Related Questions
How to handle movement in a multiplayer FPS? 0 Answers
Making character dash forward 0 Answers
2D rigidbody movement & walls 2 Answers