Physics in Unity!
Unity comes equipped with a built-in Physics Engine. It helps you simulate physics in your project to ensure that GameObjects correctly handle various effects like acceleration, gravity, collisions, and other forces.
Physics can be applied to an object through the use of a ‘Rigidbody’ and ‘Collider’ Components. One thing to mention is that Unity has two separate Physics systems(3D, 2D) that do not interact with each other - so make sure you always choose the correct one for your type of project!
I have created a new ‘3D Object’ > ‘Sphere’ and added a ‘Rigidbody’ component to it.
In the ‘Rigidbody component,’ you can set and adjust various different properties. These will allow you to define how the physics should behave for each GameObject.
- Mass - The mass of the object, by default, in kilograms. Like in real life, a GameObject with a higher mass has more of an effect on one with a lower mass.
- Drag - Amount of resistance that is applied to the GameObject. The default value of 0 means no resistance - and infinity makes the object stop moving immediately. If applied to a GameObject, unless there is some force being applied to the GameObject, it will stop any movement eventually.
- Angular Drag - Amount of rotational resistance that is applied to the GameObject. The default value of 0 means no resistance. You can’t stop the GameObjects rotation by setting the value to infinity.
- Use Gravity - If this setting is enabled, the object is affected by gravity.
- Is Kinematic - If this setting is enabled, the object will not be driven by the physics engine and can only be manipulated by a script or an animation. Use this whenever a GameObject should be static until a condition is set.
- Interpolate - This setting can help fix jittering in the movement that a fixed frame rate can cause. This option is very resource-heavy and should be left as ‘None’ unless needed.
None - Default setting. No Interpolation is applied, no resources are used.
Interpolate - Transform is smoothed based on the Transform of the previous frame. This form of Interpolate is less resource-heavy.
Extrapolate - Transform is smoothed based on the estimated Transform of the next frame. This form of Interpolate is very resource-heavy.
- Collision Detection - This setting is used to prevent fast-moving GameObjects from passing through GameObjects without detecting collisions.
Discrete - Default setting. Unless needed always leave collision detection at this level - used for normal collisions.
Continuous - Used when checking for colisions with high-speed static GameObjects.
Continuous Dynamic - Used when checking for collisions with high-speed static or dynamic Game Objects.
Continuous Speculative - Only setting that can be used on Kinematic GameObjects. Similar to ‘Continuous Dynamic’ but being less resource-heavy at the cost of accuracy.
- Constraints - This setting restricts the Rigidbody's movements.
Freeze Position - Stops the Rigidbody from moving in the world on the X, Y, and Z axes.
Freeze Rotation - Stops the Rigidbody from rotating around the local on the X, Y, and Z axes.
- Info - Contains information about values regarding your Rigidbody on the GameObject. This can be monitored in real-time(During play mode).
There is one more option when you specifically use ‘Rigidbody 2D’ - and that is the ‘Sleeping Mode.’ This option exists for optimization and can remove the GameObject from the Physics interactions when it ‘sleeps.’
Lastly, you can edit any of the default Physics values in your ‘Edit’ > ‘Project Settings.’
Updates to the Rigidbody
If you wish to make changes or apply forces to a Rigidbody component, you should do so in the ‘FixedUpdate’ method and not in the ‘Update’ method. This is because the Update method is frame dependant, and the FixedUpdate method is not and is being called every fixed frame-rate at a measured time step.
And that is all today in regards to the Rigidbody component. In the following article, we will look at the Collider component. Which, as mentioned, is also a part of the Physics engine.
But that is it for now, thank you for reading and feel free to follow me for more articles - and as always, good luck and see you next time!