Translations of this page:

Sidebar

en:vci:sample:ontrigger:no1

Change the color of a ball when it enters and exits a box


This article was written for “UniVCI-0.15.”

Example data:https://seed.online/items/80b7bd78dfa6647c6fbfdd6c4c85814bb606b882f075db42b0eceb0bfca6bcd5

To execute a process when a Collider (Trigger) collides with a SubItem, use “onTriggerEnter”.
To execute a process when a Collider (Trigger) disengages, use “onTriggerExit”.

1. Create a new GameObject and create a Cube, a Sphere and a Plane as its child.

In this example, we named the GameObject “TriggerEnterExit_VCI,” but it can be anything really.
Configure the objects as below:

  • Cube (box): Set the Y-coordinate of the Cube 1.0
  • Sphere (ball): Set the Y-coordinate of the Ball 2.0
  • Plane (ground): Set the Y-coordinate of the Plane 0.0

2. To change the color of the Cube, create a new material file “TriggerMaterial”. Set the created TriggerMaterial as the material of the Cube.

3. Attach a “VCI Object” component on the GameObject,
and attach the “VCI SubItem” component on the Cube, Ball and Plane.
When doing this, “RigidBody” components are also attached automatically.
As the Cube and the Plane won't be using gravity, turn off Use Gravity and turn on Is Kinematic.
As we need the Ball to have physics, turn on Use Gravity and turn off Is Kinematic.

4. On the VCI Object component in the GameObject, set the Scripts Size as 1.
Enter “main.lua” in the Name and paste the script shown below.

main.lua
local green = Color.__new(0,1,0,0.5)
local red = Color.__new(1,0,0,0.5)
 
function onTriggerEnter(item, hit)
    if item == "Ball" and hit == "Cube" then
        vci.assets._ALL_SetMaterialColorFromName("TriggerMaterial", red)
    end
end
function onTriggerExit(item, hit)
    if item == "Ball" and hit == "Cube" then
        vci.assets._ALL_SetMaterialColorFromName("TriggerMaterial", green)
    end
end
en/vci/sample/ontrigger/no1.txt · Last modified: 2019/07/08 18:05 by h-eguchi

Page Tools