Rigidbody and Collider Concepts in Unity

 

Hello everyone,

Today, I wanted to explain two concepts that should be learnt in the first place when starting Unity to new game development friends like me. After opening an empty project and adding that famous cube (in all the tutorials I saw, the teaching started by adding a cube :), the first thing to do is usually adding Rigidbody to the cube. So let’s start with this.

What is Rigidbody?

What is Rigidbody? Let’s examine this under this title. Rigidbody is a component used in Unity to give an object physical behaviour. Adding a Rigidbody to an object means allowing that object to be managed by the physics engine. So what is a physics engine? The physics engine simulates the movement and interaction of game objects in the physical world. This ensures that the object has physical properties such as mass, gravity, speed, force, and moves in accordance with the laws of physics. It also allows realistic motion and collision calculations. So it is very necessary for games.

What are the Basic Properties of Rigidbody?

Let’s examine the basic properties that appear in the inspector window when we add Rigidbody.

Rigidbody Inspector
  • Mass: Determines the mass of the object. Mass affects the resistance of the object against forces. Since the value we will write here means the mass of the object, objects with higher mass require more force.
  • Drag (Friction): It is the resistance force against the movement of the object. The higher the value we write here, the slower the object moves.
  • Angular Drag (Angular Friction): It is the resistance force against the rotational movement of the object.
  • Use Gravity: Enabling this option will cause the object to be under the influence of gravity, and if it is high up, it will fall downwards when the game is started. If there is no object to stop it when it falls, it will disappear from the game scene, but its presence will remain in the game system.
  • Is Kinematic: When this option is enabled, the object is not affected by the physics engine, but can move when it hits other objects. Kinematic objects are usually controlled programmatically.

What can we do using rigidbody?

Under this heading, let’s examine what we can do using the rigidbody feature.

  • Moving objects by applying force and torque. Thanks to this application, objects can fall faster, roll when falling or different reactions can occur.
  • Letting objects free fall. An object positioned higher than the ground can fall downwards when the game is started and stop when it hits the ground, or if the ground does not have a collider feature, it can pass through the ground and continue travelling in space.
  • Setting objects to react physically after collision. Post-collision disappearance function or any animation and effect can be added.

What is Collider?

Another feature that has been used in Unity since the beginning is collider. It is a component used to determine the presence of an object in the physical world. It can be called like a 3D frame placed around it. Adding Collider to an object allows that object to react to collisions. The Collider component can be of various types depending on the shape of the object. Now let’s look at which types it can be according to its shape.

What are Collider Types?

  • Box Collider: It is used for rectangular prism-shaped objects. It has a simple box shape.
  • Sphere Collider: Used for sphere-shaped objects.
  • Capsule Collider: Used for objects in the shape of a capsule (two hemispheres and a cylinder).
  • Mesh Collider: Used for complex shaped objects. It creates the collision area using the mesh structure of the object. Generally suitable for static (stationary) objects.
  • Terrain Collider: Used for terrain objects.

What are the Basic Properties of Collider?

Let’s examine the basic properties that appear in the inspector window when we add the Collider.

Collider Inspector
  • Is Trigger: When this option is enabled, Collider acts as a trigger instead of a collision. It initiates trigger events instead of physically interacting with other objects.
  • Material: A Physic Material is used to add physical properties to the Collider. This determines properties such as friction and flexibility.

What can we do using Collider?

Under this heading, let’s examine what we can do using the collider feature.

  • We can make objects collide with each other and give physical reactions. For example, when there is a collision, the objects go back separately or an animation like an explosion effect.
  • Enabling objects to initiate trigger events when they enter certain areas. For example, making an object disappear when it enters the collider area of another object.

Since we know both components separately, let’s finally examine how they can be used together.

Using Rigidbody and Collider Together

Adding both Rigidbody and Collider to an object allows us to fully manage the object’s interactions in the physical world. Since this management is a very important part of the game development process, learning these two components can make us a better game developer to begin with.

Rigidbody manages the physical movements and forces of the object, while Collider manages the collisions and trigger events of the object with other objects.

For example, let’s take a ball dropping and rolling on the ground in a game scene:

    • By adding a Rigidbody to the ball, we can ensure that it remains under the influence of gravity and moves with physical forces, i.e. rolls realistically.
  • At the same time, by adding a Sphere Collider to the ball, we can manage its collisions with other objects and add an effect or other triggered event.

In this way, the Rigidbody and Collider components work together to realistically simulate the physical behaviour and interactions of the object.

In this article, I tried to explain the importance of Rigidbody and Collider by talking about what it is. I hope it was useful.

Good coding to everyone.

Selin.

Hiç yorum yok: