Unity Rigidbody and Contact Detection

Unity

Unity Contact Detection


Hello friends, in this post I will explain how we can detect the contact between two objects in Unity.

With the Rigidbody component in Unity, we can detect these contacts very easily.
We can adjust many more things with the Rigidbody component, but I am in favor of learning these more thoroughly by showing how we use them in projects in the future.


Rigidbody

There are 2 Rigidbody components in Unity: one is Rigidbody2D developed for 2D games and 2D objects, and the other is for 3D games, called simply Rigidbody without a 2D suffix. In this article, I will summarize Rigidbody for 3D directly, as both are actually the same as each other.

Rigidbody

We add a Rigidbody to the object we want, by selecting Rigidbody type from the "Add Component" section. On our object, a panel like the one above will appear.
Mass: Mass
Drag: Friction coefficient, you can increase it to make the object stop quicker.
Angular Drag: You can use/increase to slow down the rotational speed of the object.
Use Gravity: Use gravity. When you activate it, the object will start to fall towards the ground unless prevented by a ground (Collider).
Is Kinematic: When enabled, the physics engine does not activate for the object. Generally used for moving objects (objects not controlled by Rigidbody).
Interpolate: This option is used to fix any issues with the movement of the object.
Collision Detection: You choose how the contact is detected (on contact, during contact, both during contact and while in communication with the object, etc...)
Constraints: This is the area where you can enable or disable which way the object will rotate or move by the Rigidbody.
If you do not want the object to rotate or move as a result of collisions, you can tick everything there.

Contact Detection

Now that we've provided information about Rigidbody, let's go over how to detect contacts between objects in Unity.

Unity Contact Detection 1


First, we create 2 squares and 1 sphere. Let our sphere represent the character, the square on the right represent the point box, and the square on the left represent an obstacle/enemy.
We add
a Rigidbody to our character and ensure it has any kind of Collider.

Unity Contact Detection 2

Now, for the point object, make sure it has a Collider and give it the tag "Point" in the tag section.
By the way, make sure that "Is Trigger" is not active in the Collider section. Because we will write our code using two different functions.
If Trigger is active, the code we'll write will not work.

If you're wondering what Is Trigger does, it prevents other Collider objects from passing through this object, like our character.

Unity Contact Detection 3

For the obstacle object, we assign the tag "Obstacle", but this time I want the object to be passable, so I activate "Is Trigger".

Now that we've prepared our scene, we can start writing our scripts. We will write two different scripts for these two objects.

Contact Detection 4

Create a script named "character" and attach it to the sphere object that I designated as the character.

OnCollision

It is used to detect contacts between objects that cannot pass through each other. 
There are 3 different states.
After the function is written, it takes Collision as the inner variable.
OnCollisionEnter(): Runs once when the object makes contact for the first time.
OnCollisionExit(): Runs once when the contact with the object ends.
OnCollisionStay(): Runs for as long as the object is in contact.

void OnCollisionEnter(Collision collision){
         if(collision.transform.tag == "Point"){
            Debug.Log("Contacted with Point");
            //Other desired commands..
        }
    }


OnTrigger

It is used to detect contacts between objects that can pass through each other.
Just like OnCollision, it also has 3 different states.
After the function is defined, it takes Collider as the inner variable.
OnTriggerEnter(): Runs once when contact is made for the first time.
OnTriggerExit(): Runs once when contact ends.
OnTriggerStay(): Runs for as long as the contact is ongoing.

   void OnTriggerEnter(Collider collider){

         if(collider.transform.tag == "Obstacle"){
            Debug.Log("Contacted with Obstacle");
           
        }
    }

If we were to write all our codes about contact detection
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class character : MonoBehaviour
{
    // Start is called before the first frame update

    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {

    }


    void OnCollisionEnter(Collision collision){
         if(collision.transform.tag == "Point"){
            Debug.Log("Contacted with Point");
           
        }
    }
    void OnTriggerEnter(Collider collider){

         if(collider.transform.tag == "Obstacle"){
            Debug.Log("Contacted with Obstacle");
           
        }
    }
}

That's all for our contact detection codes, friends. If you have any other questions, you can write them in the comments.