Basic Physics in Unity
Objective: To understand how Unity looks at physics, and how to implement it.
Whether you are working on a 3D or 2D game, Unity provides us with different physics engines to ensure your game objects correctly interact with acceleration, collisions, gravity, and other various external forces.
By default, when you create a new 3D game object, it comes with a Transform component and a Mesh Filter, a Mesh Renderer, and a Collider.
Its Transform tells us where the object lives in 3D space, its rotation, and its scale.
To render a mesh, you need two components: the Mesh Filter and the Mesh Renderer. The Mesh Filter is the collection of vertices (lines and points) that make up a shape composed of polygons (triangles). This is the data for the model.
The Mesh Renderer component holds the data to render the mesh in your scene. It does so by applying materials and determining how the object interacts with light sources such as casting shadows or reflecting light.
The Collider is necessary in order to detect if objects collide with each other. They may also behave as triggers that detect and initiate a secondary action (such as opening a door or setting off an ambush). If the Collider is added to an object, and it is designated as a trigger (“Is Trigger” set to TRUE), it will effectively ignore the physics engine.
The final piece of the puzzle we need is called a Rigidbody component. Rigidbodies are what allow objects to be controlled by physics and move as a result of collisions.
A Collider does not require a Rigidbody to be attached to an object in order to detect a collision, but you must have a Rigidbody component attached if you wish your game object to move within the game space as a result of a collision.
In the example below, both the Sphere and Cube have Rigidbody components attached. Only the Sphere has “Use Gravity” set as TRUE, and as a result, it falls once the Scene is in Play. Upon colliding, the Cube is set in motion, tumbling off into space as a result of its impact with the Sphere.
If we remove the Rigibody from the Cube, set the “Is Trigger” value to TRUE in its Collider component, and run the Scene once more, the Sphere will simply fall through the Cube. A collision will be detected, but the lack of Rigidbody will eliminate the physics.
There you have it. A very basic intro into the world of physic as it applies to Unity.