3. Creating Item Behavior

If you haven’t done so already, please complete the previous tutorial, starting with How to create a 3D Roll a Ball game.

Create the behavior of the Item (yellow cube).

The Item object is prepared as a prefab, and it is assumed that the object will be reused.
Here, we will not use Logic Behavior directly, but rather create a behavior in an asset type called Logic Asset, and reuse the behavior by playing it with Logic Player.

The contents to be created are as follows:

  • Destroy your object when it comes into contact with the Player using OnTriggerEnter.
  • Use Logic Asset to create the behavior and reuse the same behavior.
  • Use Logic Player to play back the behavior.

Creating assets

Creating the Logic folder

Create a folder to save Logic Toolkit related assets.
You can name it anything, but in this example we’ll create a folder called Logic.

  • Select the Assets/RollABall folder in the Project window
  • Click the + button
  • Select Folder from the menu
  • Change the name to Logic

Creating a Logic Asset

  • Select the Logic folder in the Project window
  • Click the + button
  • Select Logic Toolkit/Logic Asset from the menu
  • Change the name to Item Logic
  • Click the Edit button in the Item Logic Inspector to open the Logic Editor window

Editing Item Logic

We will edit the Item Logic in the Logic Editor window.

Creating a PubOnTriggerEnter.Callback node

Contact between the Player and Item is detected by OnTriggerEnter, so the event is executed via a component for passing the OnTriggerEnter message included in the Logic Toolkit.

  • Click on the graph to focus
  • Press the Space key to open the node creation menu
  • Select the Members tab
  • Enter OnTriggerEnter Callback in the search field
  • Select LogicToolkit.Builtin.Messages.PubOnTriggerEnter Callback from the list
  • Select Event from the node type selection menu
  • Confirm the node name with the Enter key

The components in the LogicToolkit.Builtin.Messages namespace are pre-packaged with the Logic Toolkit to accept Event nodes when a MonoBehavior message is called.
In this example, they are used to accept the OnTriggerEnter(Collider) message in the Event node.

Editing the PubOnTriggerEnter.Callback node

  • Check Require in the Target field

The field for setting a component also has a function to add and then retrieve the corresponding component if it is not present in the target object.
Since Require is checked this time, the PubOnTriggerEnter component will be added at runtime even if it has not been added to the object itself (Item).

Depending on the purpose of the object, the processing load of adding components each time may become a problem.
It is a good idea to consider in advance whether or not to add components to an object.

Creating a Component.CompareTag node

It determines whether the person in contact is a Player by comparing with the Player tag.

  • Drag and drop the output port of the Arg 0 field of the PubOnCollisionEnter.Callback node to the right side (where you want to create the destination node) and open the node creation menu.
  • Select the Members tab.
  • Select Component/CompareTag(string tag) from the list.
  • Select Compute from the node type selection menu.
  • Confirm the node name with the Enter key.

Editing the Component.CompareTag node

  • Set the Tag field string to Player

Creating a Branch Node

The node to be executed will be branched depending on the result of the CompareTag comparison.

  • Drag and drop the Component.CompareTag node to the right side of the output port in the Result field (where you want to create the destination node) and open the node creation menu.
  • Select the Scripts tab.
  • Select Flow Controls/Branch from the list.

Connecting to a Branch node

  • Drag and drop the execution port of the PubOnTriggerEnter.Callback node to the input port of the Branch node to connect it.

Creating a GameObject.Destroy node

  • Drag and drop the True port of the Branch node to the right side (where you want to create the destination node) and open the node creation menu.
  • Select the Scripts tab.
  • Enter Destroy in the search field.
  • Select GameObject.Destroy(gameObject) (Action) from the list.
  • Confirm the node name with the Enter key.

Unity’s Destroy method is Object.Destroy(Object), but it is more convenient to be able to specify your own objects, so we have included a script that performs GameObject.Destroy(GameObject).

Delete the Start node

We won’t be using the Start node that is provided by the beginning, so let’s delete it.

  • Select the Start node
  • Press the Delete key to delete the selected node.

こOnce you’ve done this, the graph will look like this:

Editing the Item prefab

Open Item in Prefab Mode

Item objects are created as prefabs.
To edit prefabs reliably, you need to open them in Prefab mode.

  • Double-click Assets/RollABall/Prefabs/Item in the Project window
    or select it and click the Open button in the Inspector window.

Add Logic Player

  • Select the Item object in the Hierarchy window
  • Click the Add Component button in the Inspector window
  • Select Logic Toolkit/Logic Player from the Component menu

Editing the Logic Player component

  • Set Item Logic in Asset field

Exit Prefab Mode and Save

  • Click the < button in the header of the Hierarchy prefab
  • A dialog box will appear asking if you want to save it, so click the Save button

Check the behavior of the item

Start playing and check the behavior of the item.

  • Verify that Item objects are destroyed when a player wants to touch them
  • Verify that all Item objects are destroyed

Next time

Next time we will cover 4. Creating the Main Logic Behavior.

Post completion

If you would like to post on social media about the completion of the work up to this point, click here.