Back To Home

Unity

23-03-2024


Taming the Physics Beast: Performance Optimization Tips for Your Unity Game

Physics are the lifeblood of many games, adding realism and dynamic interactions. But unoptimized physics can quickly bring your frame rates crashing down. This post dives into essential physics optimization techniques for Unity developers. Learn how to keep your game smooth and responsive, ensuring an exceptional player experience, no matter how crazy your physics get!

post 2

Unity uses 2 differnet physics engine for 2D and 3D. Nvidia’s PhysX for 3D physics and the open source project Box2D for 2D physics. Lets see how to use it properly to improve the overall performance of the game.

Physics System :

  1. Unity uses a physics engine to calculate the complex calculation on physics objects,solving equation of motion, detecting collision etc. physics system is like a loop that runs every few frames. Physics engines generally operate under the assumption that time advances by fixed values, and both of Unity’s physics engines operate in this manner.

  2. Each of these iterations is known as a timestep. The physics engine will only resolve each timestep using precise values of time, which is independent of how much time it took to render the previous frame. This is known in Unity as the fixed update timestep, and it is set to a value of 20 milliseconds by default.

  3. At each new time step, it detects any collisions, then calculates the velocity and position of an object. And finally, sends the location data to the GPU. This continuous loop creates the illusion that an object is falling due to gravity.

FixedUpdate :-

  1. fixed updates are processed just before the physics engine performs its update. The FixedUpdate() callback is a useful place to define any gameplay behavior that we want to be framerate independent. AI calculations are commonly resolved in fixed updates since they tend to be easier to work with if we assume a fixed update frequency.

Static colliders and dynamic colliders :-

  1. If a gameobject contains both the rigidbody and collider component then we say it is dynamic collider. If we collide one dynamic collider into another, they will both react based on Newton’s laws of motion.

  2. We can also have colliders that do not have a Rigidbody component attached, and these are called static colliders. These are like invisible colliders that we use to trigger something or to make a boundary.

Collision Detection :-

  1. The goal of a collision detection system is to report if two objects have collided.It reports a simple YES/NO answer. There are three settings for collision detection in Unity, which can be configured in a RigidBody component’s Collision Detection property: Discrete, Continuous, and ContinuousDynamic.

  2. Discrete is the default collision we get.It teleports the object a small distance every time stamp on their velocity.Then it performs the bounding volume check for any overlap, treats them as collision.

  3. Continuous is used when we detect collision on static object.

  4. Continuous Dynamic is used to detect collion between two fast moving objects.

Collision Detection :-

  1. The goal of a collision detection system is to report if two objects have collided.It reports a simple YES/NO answer. There are three settings for collision detection in Unity, which can be configured in a RigidBody component’s Collision Detection property: Discrete, Continuous, and ContinuousDynamic.

  2. Discrete is the default collision we get.It teleports the object a small distance every time stamp on their velocity.Then it performs the bounding volume check for any overlap, treats them as collision.

  3. Continuous is used when we detect collision on static object.

  4. Continuous Dynamic is used to detect collion between two fast moving objects.

Colliders :-

There are different types of colliders both for 2D and 3D objects.

  1. Box Collider

  2. Sphere Collider

  3. Capsule Collider

  4. Mesh Collider

The first three are primitive types which should be used most of the times.We can scale these colliders to fill the objetcs shape.

Mesh colliders are the most expensive collider we have.It takes the shape of the gameobject applied on. We should avoid using it and use compound colliders.

post 2

Tips :-

  1. keep all physics object scales in the world as close to (1,1,1).

  2. Prefer discrete collision detection.

  3. Disable auto-sync transforms.

  4. Enable re-use collision callbacks.

  5. Minimizing raycasting and bounding-volume checks.

  6. Avoid mesh colliders, use instead compound colliders of simple shapes.

  7. If using mesh colliders: enable read/write flag on its mesh importer.

  8. Add more layers and minimize the layer collision matrix enabled pairs.

  9. keeping all objects close to (0,0,0) in the world-space position will result in better floating-point accuracy, improving the consistency of the simulation.

  10. Do not put any floating value to mass of the object, it should be accurate.

  11. Adjust the Fixed Timestep setting (in the Time window) to reduce the time spent on physics updates.

Congratulations! You've officially tamed the physics beast in your Unity game. Now your players can enjoy epic explosions, mind-bending acrobatics, and physics-based mayhem without their frames taking a vacation. Remember, a well-optimized game is a happy game (and a happy developer who doesn't have to hear complaints about laggy physics)!

I hope you found this blog post enjoyable!


More Blogs

See All