Day 6: Physics in Unity

Rigidbody

Rigidbody is the component that we attach to the game objects that we want Unity to simulate physics on.

Rigidbody component is added with “Use Gravity” checked by default. By clicking Edit — Project Settings — Physics we can reach to the Physics settings of Unity. The game objects that are added Rigidbody component with “Use Gravity” checked will be manipulated by the force of the physics engine according to these settings. By default, Gravity in the settings is set to -9.81 on the Y direction, just like as in our world.

If we uncheck “Use Gravity” on the Rigidbody field, the game object will not be effected from the gravitational force and stand still if there is no other force effecting it. Here’s an example of a game object with “Use Gravity” is unchecked at first, and checked afterwards. As we turn on the gravitational force, it falls down.

Use Gravity: Checked

Mass, simply denotes the weight of the object that we have added Rigidbody component. Irrelevant of the size (scale) of the game objects, it is used simulate the physics interactions between the objects. For instance, we need to simulate a truck is heavier than a car by manipulating its mass component, not the scale. A heavier object will result in more effect applied to an object with a smaller mass.

Drag component denotes the air resistance applied to the object. It is not related to gravity. Thus, if we increase drag, it will fall slower to the ground denoting that air resistance will make a parachute-like effect, according to the value. For instance, if we keep the “Use Gravity” checked and increase the Drag from 0 to 10, the object will lose attitude slower:

Use Gravity Checked, Drag: 10

Whereas angular drag is for slowing down the rotation of the object. Angular Drag and rotation is inversely correlated, meaning that if we increase the angular drag, it will decrease the rotation of the object. If you leave two bottles, one empty and one full, from an angled surface, you will notice that they will have different rotations while going down.

“Is Kinematic” in the rigidbody is used for deciding if the game object will be effected from forces applied by other objects or not. By checking “iskinematic”, we make our object to be prevented from the physics effects that is applied by other game objects. However, the object we are on is going to still effect other game objects with rigidbodies or colliders. Imagine one of the first video games: Pong. In Pong, we can make the paddle that we are controlling kinematic and control it by script by manipulating its transform position values. Despite our paddle will not we effected from the force applied from the ball, it will apply forces onto the all.

Pong game

Colliders

Colliders are the crucial components that defines the physical collision shape of the game objects. Rather than the actual shape of the game objects, the forces that are going to apply to a specific game object will apply according to their collider shapes. It is crucial to denote that collider shapes are not always has to be exactly the same as the shape of the game objects, as colliders are invisible in the game scene.

Unity suggests us to use primitive colliders such as Box, Sphere and Capsule Colliders in 3D and Box, Circle Collider 2D for the 2D games. Even a mix of these colliders on a single game object would be preferable than a mesh collider. Why is that?

Mesh colliders are being used for game objects that have irregular shapes that we are unable to represent by the primitive colliders. Mesh colliders will require more computational force from the processors than a primitive one.

By clicking ‘Edit Collider’ or manipulating the values on ‘Center’ and ‘Size’ fields, we can easily control and change the box collider shape and size.

Collisions

In Unity, the Collider components are being used in order to detect collisions. However, colliders do not have the power to decide on how will the collisions effect each other. Thus, if we have 2 game objects colliding, we need Rigidbody on at least one of them in order to detect a collision.

There are 2 types of collisions in Unity :

- Hard Surface

- Pass Through (Trigger)

Hard surface collisions are the collisions that results in applied physical forces on one or both of the game objects that are being collided. Whereas Pass through (trigger) collisions do not create forces effecting each other but trigger an event. In general, pass through collisions are used for creating illusions in the games such as collecting a coins or power-ups.

By default, our scripts that we created from Unity inherit from the MonoBehavior class, which includes predetermined functions for collisions and triggers:

- OnCollisionEnter

- OnCollisionStay

- OnCollisionExit

- OnTriggerEnter

- OnTriggerStay

- OnTriggerExit

Certainly, these methods are for 3D games. For 2D games, we just add 2D at the end of each simply like OnCollisionEnter2D.

Here, “other” gathers and stores information regarding the other game object that has entered a collisions with the one we are working on. We can manipulate the other game object easily. For instance, we have created the illusion of a game object that has been destroyed because it has been shot. Since we do not want a hard surface collision, we have used onTriggerEnter.

Red game object’s script has been provided below:

private void OnTriggerEnter(Collider other){if(other.gameObject.tag == “Laser”){ Destroy(other.gameObject);
Destroy(this.gameObject);}
}

In order to make this possible, we have created new tags and tagged the necessary game objects accordingly. It is crucial to make sure that we destroy the game object that we are on at last, so that other commands will not be left in the half-way.

We can manipulate the game objects that we are colliding even more by referencing them. Referencing, basically enable us to create communication between two different scripts.

In our case, let’s assume that we want the Enemy space vehicle will result in a damage to the player if it collides with it. First of all, we need to make sure of the object we are collided with by checking its’ tag. After we made sure that we have collided with Player, we create an instance of it with the command

Player player

First, “Player” with capital P denotes the type of the game object. player with lower case ‘p’ is the instance we have created.

In Unity, GetComponent command allows us to reach any component attached to a game object. In order to create the communication between the scripts, we need to reach the script that we want to access in this case. Since, the name of the script attached to our Player is also named “Player”, we need to access it.

It is always the best practice to make sure that the target instance that we wanted to access is not null as null reference exception errors are probably of the most encountered errors in Unity.

After making sure that we have a non-empty instance of an object on our hands, we can trigger the required method to run.

The code on the Enemy (red object) script is provided below:

Unity Developer