Introduction to Unity Visual Scripting Part 6: How to add collision detection with Collision

This article is the sixth installment of the Introduction to Visual Scripting series. You can check the previous article from the link below.

This time, we will explain how to implement collision detection using Collision.

Collision-Based Hit Detection

Add an empty GameObject to the Hierarchy and name it [HitManager].

[HitManager]

[HitManager]

Attach a Script Machine to [HitManager], create a new graph in a folder called Macros, and save it there.

Name the graph [HitGraph].

Attach Script Machine to [HitManager], save it in the Macros folder, and rename the graph to [HitGraph].

Attach Script Machine to [HitManager], save it in the Macros folder, and rename the graph to [HitGraph].

Click the + button in the Hierarchy and create a Cube from 3D Object. Name it [TopObj].

[TopObj]

[TopObj]

Set the Position of [TopObj] to (0, 5, 0), and attach a Rigidbody from AddComponent.

Make sure to uncheck the Is Trigger option in the Box Collider.

Attach Rigidbody and uncheck Is Trigger in Box Collider.

Attach Rigidbody and uncheck Is Trigger in Box Collider.

Next, add another Cube to the Hierarchy and name it [UnderObj].

Set the Position of [UnderObj] to (0, 0, 0).

[UnderObj], (0, 0, 0)

[UnderObj], (0, 0, 0)

Preparation is complete.

Add a GameObject type variable [HitObj] to the HitGraph’s Variables in [HitManager], and set its Value to TopObj.

Set Value to TopObj

Set Value to TopObj

Add On Collision Enter, Debug.Log, and String Literal to the Graph Editor, and add [HitObj] from Variables.

Add On Collision Enter, Debug.Log, String Literal

Add On Collision Enter, Debug.Log, String Literal


Add On Collision Enter, Debug.Log, String Literal

Add On Collision Enter, Debug.Log, String Literal


Add On Collision Enter, Debug.Log, String Literal

Add On Collision Enter, Debug.Log, String Literal

This Collider is what will be used for collision detection.

Collider

Collider

On Collision EnterThis node is executed when objects with Colliders collide.On Collision StayThis node is executed continuously while objects with Colliders are in contact.On Collision ExitThis node is executed when objects with Colliders separate.

The left port connects to the object that is involved in the collision.

The right port contains information about the object that the Collider has collided with.

The Contacts port stores the collision points, normals, and the two colliders involved in the collision.

The Impulse port stores the impact of the collision.

The Relative Velocity port stores the relative speed of the other object at the time of collision.

The Data port contains information about the other object.

Connect the nodes as shown below. In this case, we will have On Collision Enter execute when something collides with HitObj.

Connect nodes

Connect nodes

Run the program.

Displaying the collided object's information [UnderObj] in the Console

Displaying the collided object’s information [UnderObj] in the Console

When objects collide, the information of the collided object [UnderObj] is displayed in the Console.

Hit Detection Using Triggers

Check the Is Trigger box on the Box Collider of [TopObj].

Check Is Trigger

Check Is Trigger

By checking Is Trigger, the objects will no longer collide with each other and will pass through each other.

Add On Trigger Enter to the Graph Editor.

Add On Trigger Enter

Add On Trigger Enter

Disconnect On Collision Enter and connect the nodes as shown below.

Connect nodes

Connect nodes

Since Trigger does not involve actual contact, there is no information such as collision points or impacts.

On Trigger Enter This node is executed when objects with Colliders touch each other.
On Trigger Stay This node is executed continuously while objects with Colliders are in contact.
On Trigger Exit This node is executed when objects with Colliders separate.

Run the program.

Displaying the collided object's information [UnderObj] in the Console

Displaying the collided object’s information [UnderObj] in the Console

When the objects touch each other, the information of the touched object [UnderObj] is displayed in the Console.

This time, we covered how to create collision detection.

In the next session, we will introduce Raycast and List.

You can check the next article from the link below.