Book Image

Learning Unity Physics

By : K. Aava Rani
Book Image

Learning Unity Physics

By: K. Aava Rani

Overview of this book

<p>Unity is a powerful game development engine that provides rich functionalities to create 2D and 3D games. Developers get the opportunity to build cross-platform mobile and desktop games from scratch. With the increasing interest in using Physics in interactive development, Unity3D offers a cutting-edge platform for simulation, game development, and application development through a user-friendly interface. You will learn the fundamentals of Physics in game development. The book starts by providing a quick introduction on how we can use Physics for interactive development. Furthermore, it provides a detailed description on the uses of Physics material in Unity3D and how we can use different colliders for interaction. The book also focuses on Rigidbodies and Joints, its types and properties. There is also a dedicated section on how we can optimize applications and games if we use Physics in Unity3D. By the end of this book, you will be capable of successfully developing Physics-based simulations, games, and applications.</p>
Table of Contents (14 chapters)

Physical simulation in Unity


We will now look at the physical simulations already available in Physics and how Unity uses them. This section will also give you an overview of the built-in Physics components in Unity3D. Let's take a look at the Physics simulation in Unity:

  • Unity is a powerful tool. It is able to take care of many problems involved with interactive physical simulations. It embeds a state-of-the-art Physics engine called PhysX.

  • It's Rigidbody is mostly targeted at rigid objects.

Using Unity we can make Physics approximations based on an object's parameter values.

Built-in Physics in Unity3D

As I mentioned earlier, Unity is a powerful engine that has a number of built-in Physics components. It handles physical simulations. By adjusting a few parameter settings, we can create an object that behaves in a realistic way.

By controlling Physics from scripts, we can give an object the dynamics of a vehicle, machine, cloth, and so on. The built-in components are very useful in fast development. In most interactive developments, these simulations are required. By making Physics a built-in component, Unity3D has made developers' lives easier.

Note

Unity has two separate Physics engines: one for 3D Physics and one for 2D Physics.

As such, there is a separate Rigidbody component for 3D Physics and an analogous Rigidbody2D component for 2D Physics.

Now, let's explore the built-in components in Unity3D.

Built-in Physics components in Unity3D

The following figure depicts the basic built-in Physics components in Unity3D, which help us in interactive development:

Rigidbodies

We have already discussed that in simulation or interactive development, the most important component is a Rigidbody. It enables the physical behavior of an object. The object to which a Rigidbody is attached can be made to respond to gravity. If we want to create a ball and want it to respond to gravity, we need to add a Rigidbody component to the object and gravity will be enabled by default.

A Rigidbody component takes the movement of the object to which it is attached; therefore, we shouldn't try to move it using a script by changing the position and rotation. Instead, we can apply forces to push the object and let the Physics engine calculate the results.

Kinematic motion and Rigidbodies

Sometimes, it is desirable for a Rigidbody object's motion to not be controlled by the Physics engine but by the script code instead. This type of motion produced from a script is known as kinematic motion.

Is Kinematic is one of the properties of a Rigidbody that will remove it from the control of the Physics engine and allow it to be moved using a script. We can change the value of Is Kinematic from a script to switch this property on and off for an object by using both the script and the inspector.

Note

Is Kinematic is useful, but we should keep in mind that enabling it will affect performance. If enabled, the object will not be driven by the Physics engine and can only be manipulated by its transform, which is more performance consuming.

Colliders

It's one of the most important built-in components of Unity3D. A collider component is used to define the shape for the physical collision. We use different colliders according to the shape of the objects. A collider, which is invisible, need not be matched exactly to the shape of the object's mesh.

In the later chapters, we will see how to use different colliders.

In 3D, the following are the basic colliders:

  • Box Collider

  • Sphere Collider

  • Capsule Collider

  • Mesh Collider

Take a look at the following figure:

There are differences between 3D colliders and 2D colliders. In 2D, the following are the basic colliders:

  • Box Collider 2D

  • Circle Collider 2D

  • Polygon Collider 2D

Take a look at the following figure:

Apart from the aforementioned core colliders, there are a few important terms about the colliders that you need to know.

Static colliders

Colliders can be added to an object without a Rigidbody component in order to create floors, walls, and so on. These are referred to as static colliders. Repositioning static colliders by changing the transform position will impact the performance of the Physics engine heavily.

Note

To improve the performance, we should not reposition static colliders by changing the transformation position.

Dynamic colliders

Colliders attached to a Rigidbody object are known as dynamic colliders. Static colliders do not respond to collisions with dynamic colliders with any movement. In the later chapters, we will learn about the aforementioned colliders and how to implement these colliders in detail.

Physic Materials

Different materials are used for different objects. As the colliders interact, their surfaces need to simulate the properties of the material that they are supposed to represent. We can configure the friction and bounce using Physic Materials.

Again, Physic Materials for 2D and 3D are different; they are called Physic Materials 3D and Physic Materials 2D.

Triggers

In scripting, we can detect when the collisions are going to occur and then we can initiate taking actions using the OnCollisionEnter function. We can configure a collider, which does not behave as a solid object, as a trigger using the Is Trigger property of Unity3D, and we will simply allow other colliders to pass through. When a collision occurs, a trigger will call the OnTriggerEnter function on the trigger object's scripts. Using these functions, we can handle a number of scenarios where an action after the collision is required.

Note

The following points will give you sources where you can find more details on this particular topic:

Joints

Often, one Rigidbody object is attached to another using joints. Unity provides different joints to help us in different scenarios. We can attach one Rigidbody object to another or to a fixed point in space using a joint component. If we want a joint to allow at least some freedom of motion and so on, then Unity provides different joint components that enforce different restrictions.

The following figure depicts the types of joints:

Joints also have other options that enable specific effects; for example, we can set a joint to break when the force applied to it exceeds a decided limit.

We will learn more about joints in the later chapters.

Character controllers

In game development, often a character is required, and for this, a controller is always required. A character in a first- or third-person game will often need some collision-based Physics so that character doesn't fall on the floor or walk over the walls.

Unity3D provides a component to create this behavior, which is called CharacterController. This component uses a Capsule Collider. The controller has functions to set the object's speed and direction.

Scripting based on collision

Let's see how we can handle collision and an after-collision effects with scripting. Scripts are attached to the objects in order to call some functions on collision. We can write any code in these functions to respond to the collision event. For example, we might play a sound effect when a ball hits an obstacle.

  • OnCollisionEnter: This function indicates that the collision is detected in the first update

  • OnCollisionStay: This function indicates that during the updates, the contact is maintained

  • OnCollisionExit: This function indicates that the contact has been broken

Similarly, for Trigger Colliders, these functions are called OnTriggerEnter, OnTriggerStay, and OnTriggerExit.

Note

For 2D Physics, there are equivalent functions with 2D appended to the name; for example, OnCollisionEnter2D.

With normal nontrigger collisions, there is an additional requirement that at least one of the objects involved must have a nonkinematic Rigidbody. To create a nonkinematic Rigidbody, we must set Is Kinematic to off.

Frictionless Physic Materials

Sometimes, during the development, we require frictionless Physic Materials. Let's try to create a frictionless material. In Unity3D, to create a completely frictionless material, we need to create a new Physic Materials asset in the Project view and set its properties as follows:

In the previous image, we can see various parameters, using which we can fulfil our frictionless game requirement.