Hallo!
I’m building a simulator in which I want to move many objects quickly. Currently I am moving my objects with transform.Translate. The problem is that my frame rate drops at around 2000 objects. The presentation of the objects is not important to me, only the performance.
Does anyone have any ideas how I can improve the performance of my game?
Is Unity even suitable for this type of simulation?
What has this to do with physics then? If you’re updating the Transform, you’re bypassing that being done by the physics system i.e. the Rigidbody(2D) moving then updating the Transform. Not even sure what physics objects you are using.
Which makes me wonder why you’re driving stuff via the Transform which is what the rendering components use. The Rigidbody(2D) pose (position/rotation) is stored on those components. Move via their API.
Not sure what “type” of simulation you mean. Sounds like you’re doing things in a bad way and it isn’t scaling well.
The profiler will tell you where your time is being spent if you’re committed to your approach.
Hi, thanks for your Help!
I only move a cube, it doesn’t have to contain any physics like gravity. It just has to follow a given line.
Sorry, I am new to Unity, what do you mean with “Move via their API”?
Lets say, I want to build a warehouse in which objects can be transported around.
I am pretty sure I am doing things in a bad way. Thats why I am searching for a better solution.
EDIT: Its a 3D Simulation
So you need a Kinematic body then because it sounds like you’re driving it explicitly with no collision response. You’re on the physics forum so I can only assume these have colliders on them too.
The Rigidbody has an api to move it, use that. You can change its velocity, add forces to change velocity, set the position/rotation directly, use MovePosition/MoveRotation (for Kinematic bodies). After the simulation step and the body has moved then it writes the pose to the Transform. This is typically how physics interacts with the Transform i.e. its in control of it writing to it. By changing the Transform directly you’d doing the opposite and physics is playing catch-up.
In the end, your question is too general so look at the profiler and it’ll tell you where time is being spent.
I’d try the simplest case possible:
- All the objects being standard Cube objects.
- Ensure every cube’s GameObject contain the components MeshFilter and MeshRenderer only (and maybe the script you use to move the cube). This way we’ll avoid other components or systems to consume resources unnecessarily.
- Ensure you’re calling transform.Translate only once per cube within an Update method.
- Ensure there’s no other GameObject or script in your scene doing additional stuff.
- Additionally, you may use the Unity Profiler (menu Window > Analysis > Profiler, or Ctrl+7) to figure out where the computation time is being spent.
If you still get performance issues, then you may have reached the performance limit in your device for that number of cubes.
An alternative is trying Unity’s experimental DOTS/ECS technology, which is specifically designed to provide outstanding performance when dealing with a large number of objects. However, DOTS/ECS is a different complex engine that works in a totally different way. Also, it’s still in experimental stage and it’s still unclear when it will be released (and if it will be released at all).