[Unity/Playmaker]How to Cause an Event to Occur when the Controller Collides with an Object(COLLISION EVENT)

This article shows how to trigger an event when the controller collides with an object.

Using collide function, it enables you to cause a variety of interactions, such as changing color, rotating, or changing size of the objects.

Two main types of collisions are; the TRIGGER EVENT and the COLLISION EVENT.

This issue helps you to figure out about COLLISION EVENT.

TRIGGER EVENT

TRIGGER EVENT

TRIGGER EVENT is here.

Advanced preparation

First of all, install Playmaker, a plugin for Unity.

For the new users of  Playmaker, please read the following articles for the better understanding.

[Unity] Introduction to PlayMaker: ”Super” from Zero, Basic Knowledge

Overview

In Unity, the interaction is triggered by firing an event when the controller and object hit each other.

This is implemented by Playmaker.

There are two types of events that are triggered by an object and a hit.

  1. TRIGGER EVENT: Events triggered by “Touch”. The interaction is triggered by the overlapping information between the controller and the 3D model.
  2. COLLISION EVENT: Events Caused by “Collisions”. The interaction is triggered by information about the physical action of the collision detection with the controller.

In this issue, the COLLISION EVENT will be explained.

About COLLISION EVENT

The conditions which a COLLISION event can be triggered are as follows;

  • Rigidbody is assigned to the controller, one of the objects, or both.
  • Both the controller and the object are given Collider.
  • The Is Trigger of the Collider for both controller and object are off.
  Controller Objects
Rigidbody

Necessary
(As long as
it’s granted to either of them.)

Necessary
(As long as
it’s granted to either of them.)

Colider Necessary Necessary
Is Trigger in Collider Unchecked Unchecked

When this condition is met, “COLLISION ENTER” will be triggered on the Playmaker.

Conditions for using COLLISION EVENTS in STYLY

In order to use COLLISION EVENTS in STYLY, the following settings are required;

The object which the event is to be triggered

The following settings are required for the object that you want to trigger the event;

  • Granting Collider
  • Unchecking the Is Trigger in Collider

STYLY’s controller

You need to add the following objects to your STYLY controller;

  • Granting Rigidbody
  • Granting Collider
  • Unchecking the Is Trigger in Collider

See how to make it. Before going on furtherer, here are some explanations for the process flow;

  1. Prepare a 3D model
  2. Grant Collider to the colliding 3D model
  3. Set up interaction with 3D models in Playmaker
  4. Create an object for the STYLY controller
  5. Attach an object to a STYLY controller

Prepare a 3D model

Let’s prepare a 3D model to implement the interaction. This time we will use Cube.

Right-click in Hierarchy and select “Cube in 3D Object”.

Create a Cube

Create a Cube

This time, it will be a 3D model that implements the interaction.

Add a Collider to the Cube to give it a collider.

Granting Collider to the colliding 3D model

Collider is a component that calculates the collision detection. By assigning this information to the target 3D model, the “collision” information will be obtained when the target 3D model is touched by another 3D model.

Collider

Collider

Normally, Cube and Sphere created on a scene in Unity are granted by default, but imported 3D models need to be granted.

When you wish to add a component, add it from “Add Component” in the Inspector.

Select the Collider according to the shape of the model as you wish

Select the Collider according to the shape of the model as you wish

By adding Collider, the 3D model can now be judged as a collider.

To trigger a COLLISION EVENT, leave the Is Trigger checkbox unchecked in the Collider.

Uncheck Is Trigger

Uncheck Is Trigger

Set up interaction with 3D models in Playmaker

This time, when the controller hits a 3D model (Cube), a TRIGGER EVENT is triggered and the interaction changes color.

In advance, create a material to change the color.

Right click on Project and select ‘Create > Material’ to create a material.

Create Material

Create Material

Once you created the Material, name it RED.

Change the name and change the color within Inspector.

Change the color

Change the color

The material is now ready to be used.

The next step is to prepare the interaction in Playmaker.

With Cube selected, right-click on the PlaymakerEditor and select “Add FSM”.

Add FSM

Add FSM

State1 is added; rename State1 to “Waiting for Collision”.

Change your name

Change your name

With the State of Waiting for Collision selected, right-click and select [Add Transition > System Events > COLLISION ENTER].

Select COLLISION ENTER

Select COLLISION ENTER

Next, right-click on the PlaymakerEditor and select Add State. Set the action for this state to change the color.

Add State

Add State

State1 has been added. Let’s change the name to “Change Color”.

Change name

Change name

Set the action for Change Color, and select “Action Browser” in the lower right corner of the State tab to open the browser.

Open the Action Browser

Open the Action Browser

Select “Set Material” from the Action Browser and select “Add Action To State” at the bottom right to add the action.

Add Set Material

Add Set Material

Set the action of Set Material.

In the Set Material section, set the previously prepared “RED” material to “Material”.

Set Material

Set Material

Finally, connect the state of “Waiting For Collision” with the state of “Change Color”.

Connect the State

Connect the State

Now it is possible to set up the interaction with the Cube’s Collision Event.

Next, configure the Collider to be attached to the controller.

Create an object for the STYLY controller

Create an object for the STYLY controller that fires COLLISION events.

Image drawing

Image drawing

The controller is on the right and left, but the basic creation procedure is the same.

Proceed with the instructions for the right controller.

Create the following configuration objects;

  • Granting Rigidbody
  • Granting Collider
  • Unchecking the Is Trigger in Collider

First of all, create an empty object in Hierarchy with “Create Empty”

Change the name to the following

Right Controller: RightControllerCollider
Left Controller : LeftControllerCollider

Create RightControllerColider

Create RightControllerColider

From Add Component, add Sphere Collider and set the Radius of Sphere Collider to 0.05.

The Is Trigger is unchecked, so leave it alone.

Granting Sphere Colider

Granting Sphere Colider

The Coliider is now set up.

From the Add Component, add a Rigidbody and uncheck Use Gravity.

Granting Rigidbody

Granting Rigidbody

An object for the STYLY controller that fires a COLLISION EVENT is completed.

Attaching an object to a STYLY controller

It is required to attach the object that fires the COLLISION EVENT to the STYLY controller.

Process Flow;

  1. Find game object to find the STYLY controller
  2. Get the spatial coordinates of the controller with Get Position
  3. Set Position sets the object’s position to the position of the STYLY controller
  4. Attach objects to the STYLY controller with Set Parent

Add an FSM to the ControllerColliderRight
From the Add Component, add the Playmaker FSM
Click on the Edit button in Playmaker FSM to open the PlaymakerEditor

Adding Play Maker FSM

Adding Playmaker FSM

Select State 1 and open it by selecting Action Browser on the bottom right of the State tab.

Open the Action Browser

Open the Action Browser

Add the Find Game Object / Get Position / Set Position / Set Parent from Action Browser.

Be sure to put them in the same order as the images. It may not work properly if they are in the wrong order.

Four additional actions

Four additional actions

Find game object to find the STYLY controller

Enter the following in the Object Name of the Find Fame Object (enter the bolded part).

  • Attaching an object to the right controller: Controller (right)
  • Attaching an object to the left controller: Controller (left)

As above, a half-size space will be placed between the Controller and (right).

With the wrong letter, even one, it won’t work, so be careful.

Next, open the Store menu bar and select “New variable…” and enter the following (please enter the bolded part).

  • Right controller : right controller
  • Left Controller:leftController

The controller in STYLY will be searched for and the rightController variable will contain the controller information in STYLY.

Find Game Object settings

Find Game Object settings

Since we don’t use With Tag, use Untagged instead.

You’re done setting up the Find Game Object.

Find Game Object settings

Find Game Object settings

Get the spatial coordinates of the controller with Get Position

Click the ↕ button on the Game object in Get Postion to change it from Use Owner to Specify Game Object.

Click the = button on the menu bar and the ↕ button in the order of ↕, and specify the variable rightController in the Find Game Object’s Store.

Click the ↕ button on the Vector menu bar, select “New variable…” and enter the following (enter the bolded part).

  • For the right controller : rightControllerPosition.
  • For the left controller: leftControllerPosition.

The spatial coordinates of the STYLY controller will now be stored in the rightControllerPosition variable.

Space should be set to World.

Configuring Get Position

Configuring Get Position

Set Position sets the object’s position to the position of the STYLY controller

Click the ↕ button on the Game object in Set Postion to change it from Use Owner to Specify Game Object.

Click the = button on the menu bar and drag and drop the ControllerColliderRight from Hierachy into the None(Game Object) field.

Set the ControllerColliderRight to the Game Object

Set the ControllerColliderRight to the Game Object

Click the ↕ button on the Vector menu bar and select the rightControllerPosition set in Vector under Get Position.

Change Space to World.

Setting the Set Position

Setting the Set Position

The object is now set to the same position as the STYLY controller.

Attach these objects to the STYLY controller with Set Parent

Set Parent allows you to set the STYLY controller to be the parent and the object to be the child of the controller to follow it.

Click the ↕ button on the Game object in the Set Parent to change it from Use Owner to Specify Game Object.

Click the = button on the menu bar and drag and drop the ControllerColliderRight from Hierarchy into the None(Game Object) field.

Set the Parent.

= button click, ↕ button in that order, and select rightController.

Setting the Set Parent

Setting the Set Parent

You can now set the object to stick to the STYLY controller.

Upload to STYLY

Let’s pre-fabricate your object and upload it to STYLY.

Prefab to upload.

  • ControllerColliderRight
  • ControllerColliderLeft
  • Cube

For more information on how to upload, please click here.

Sample Scene.

You can check the operation here.

Let’s implement a variety of actions!

×