en:vci:sample:onuse:no1

Reset position and rotation on grip button press

This article was written for “UniVCI-0.15.”

Example data

https://virtualcast.jp/products/d49d7c585f3b26547f6a38dc727a77e3c28ecc45f881a20ecb1271eb6757988f

To execute a process when the grip button is pressed, use “onUse”.

In this example, we'll create a process that:
Where a specified VCI Object has multiple mobile “VCI SubItems”,
pressing the grip button against a specified VCI SubItem resets the position of all SubItems.

The example VCI has a flat box and multiple balls above it.
After you grabbed the box and moved it, you can reset the position
of the balls by pressing the grip button.

1. Create a new GameObject and create a Cube and five Spheres as its children.
(The example have only 5 spheres, but you can add as many as you like)

In this example, we named the GameObject “ResetSample_VCI,” but it can be anything really.
Name the Cube “Cube”, and the Spheres “Ball1 through Ball5”.
Specify Y-axis value of the Cube to 1.0 and for the Spheres 1.5, so that they don't go through the floor.
Also, modify the scale of the Cube to make it flatter. As for the scale of the Spheres, adjust them so that they can fit within the plane above the cube.

2. Attach a “VCI Object” component on the GameObject,
and attach “VCI SubItem” components on the Cube and the Spheres.
When doing this, “RigidBody” components are also attached automatically.
As the Cube won't be using gravity, turn off Use Gravity and turn on Is Kinematic. Also, turn on “Grabbable” in “VCI SubItem” to make it grabbable and movable.
Leave the parameters of Spheres as they are.

3. In addition, change the “GroupId” of “VCI SubItems” to 1
so that they go back to the initial position no matter who presses the grip button.
This allows you to manipulate all SubItems with GroupId 1 together.

* For details, refer to Details of VCI components and GroupID.

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 Cube = vci.assets.GetSubItem("Cube")
local Balls = {...}
local Balloffsets = {...}
for i=1,5 do
    Balls[i] = vci.assets.GetSubItem("Ball"..tostring(i))
    Balloffsets[i] = Balls[i].GetLocalPosition()
end
 
function onUse(use)
    if use == "Cube" then
        local Cube_localPos = Cube.GetLocalPosition()
        local Cube_localRot = Cube.GetLocalRotation()
        local rotationAngles = Cube_localRot.eulerAngles
        --Adjustment of the balls
        for i=1,5 do
            local relativeDirection = Balloffsets[i]
            local goalPosition = Quaternion.Euler(0, rotationAngles.y, 0) * relativeDirection
            Balls[i].SetLocalPosition( goalPosition + Cube_localPos )
            Balls[i].SetVelocity(Vector3.zero)--Make the velocity of the RigidBody 0
            Balls[i].SetAngularVelocity(Vector3.zero)--Make the angular velocity of the RigidBody 0
        end
    end
end
en/vci/sample/onuse/no1.txt · Last modified: 2023/05/11 19:56 by pastatto

Page Tools