Performance: Why physics simulate realistic behavior
Physics simulation affects frame rendering speed and input responsiveness by consuming CPU resources during game updates.
Jump into concepts and practice - no test required
void FixedUpdate() {
foreach (var obj in activePhysicsObjects) {
if (obj.IsAwake) {
obj.ApplyForce(CalculateSimplifiedForce(obj));
obj.UpdatePhysics();
}
}
}void Update() {
foreach (var obj in allPhysicsObjects) {
obj.ApplyForce(CalculateComplexForce(obj));
obj.UpdatePhysics();
}
}| Pattern | CPU Usage | Frame Drops | Input Delay | Verdict |
|---|---|---|---|---|
| Full physics on all objects every frame | High | Frequent | High | [X] Bad |
| Physics only on active objects in FixedUpdate | Low to Medium | Rare | Low | [OK] Good |
Rigidbody component to a game object in Unity's physics system?AddForce, not ApplyForce or others.AddForce takes a Vector3 direction multiplied by force magnitude, like Vector3.up * 10.void Start() {
Rigidbody rb = GetComponent<Rigidbody>();
rb.useGravity = false;
rb.AddForce(Vector3.up * 20);
}useGravity = false disables gravity effect on the Rigidbody.AddForce(Vector3.up * 20) pushes the object up.void Update() {
Rigidbody rb = GetComponent<Rigidbody>();
rb.AddForce(Vector3.forward * 10);
}