Learning Unity — Introduction To Physics In Unity

In the last article, we took our first look at using time within Unity to control logic in our scripts. In this article, we’ll be taking our first look at Unity’s physics engine.

The basis of all physics engine interactions in Unity are two components that can be added to any game object through the editor — Colliders and Rigidbodys.

Collider is the component that defines the shape of a game object for purposes of physical interactions between objects. Game objects are typically created with a default Collider. Colliders can be of two types — Collisions or Triggers. We’re going to go more in-depth on the distinction between Collisions and Triggers in the next article, but in short Collisions are for when you want objects to interact with each in a physical way and Triggers are for when you simply want to know if an object has entered a space.

Here is a Collider component from the Inspector window. Specifically, this is a Sphere Collider, which is one of the 3D primitive types of Colliders, along with Boxes and Capsules.

The options are fairly straight forward for the Collider. The Center and Radius fields allow you to fine tune the size and shape of the Collider. The Edit Collider button allows you to do both of those things as well, except it allows you to do them by manipulating points within the Scene window. The Is Trigger checkbox allows you to set whether or not the Collider will be of the Trigger type.

Finally, the Material field allows you to specify a Physic Material. I’m not going to get into a full explanation of Physic Materials in this article, but in brief they allow you to specify certain physical aspects of your game objects, such as bounciness and friction.

Rigidbody is the component that does the “heavy lifting” (pardon the pun) for the Unity physics engine. A Rigidbody is required for any object that is expected to react realistically to being pushed, dropped, or otherwise struck by other objects with a Rigidbody.

Here is a Rigidbody component from the Inspector window. As you can see, it’s a bit more complex than the Collider. Going over every option available here is outside the scope of this article, but I did want to briefly two go over two of these.

The first is the Use Gravity checkbox. This, as you might expect, determines whether or not the object is affected by gravity. This is important to note because it is set to true by default. In case you were wondering why your objects were sailing away when you start your game up, this might be why.

As a side note, the direction and force of gravity can be altered in the Build settings of your project.

The second option I wanted to touch on was the Is Kinematic option. This option, when switched on, means that the object is not driven by the physics engine and can only be moved through its Transform.

Before we wrap up this brief introduction to Unity’s physics engine, I wanted to give you a quick demonstration of the physics in action. I created a very simple scenario of a sphere with a Rigidbody positioned over two cubes shaped into something of a ramp.

Fun, huh? To watch this and realize that the power of this engine is at my fingertips is really inspiring.

Anyway, like I mentioned earlier, the next article will be covering the distinction between Collisions and Triggers in more depth. Until then, good luck and happy coding!