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].
Attach a Script Machine to [HitManager], create a new graph in a folder called Macros, and save it there.
Name the graph [HitGraph].
Click the + button in the Hierarchy and create a Cube from 3D Object. Name it [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.
Next, add another Cube to the Hierarchy and name it [UnderObj].
Set the Position of [UnderObj] to (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.
Add On Collision Enter, Debug.Log, and String Literal to the Graph Editor, and add [HitObj] from Variables.
This Collider is what will be used for collision detection.
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.
Run the program.
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].
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.
Disconnect On Collision Enter and connect the nodes as shown below.
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.
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.