en:vci:sample:transform:no3

Make an item appear and disappear (by changing scale)


In this example, “Target” SubItem appears/disappears when the “Switch” SubItem is used. This is the pattern where the object is not actually destroyed, but making it seems it's gone by shrinking its scale.


Material data

We don't use any pre-made materials.
We will be using primitives in Unity.

Component setting


Basically, all you have to do is to create two grabbable SubItems, but here are some catches:

Set GroupIDs to a value other than 0
It's better to have the ownership on your side when changing transforms.
For details on how GroupID works, refer to The role and usage of SubItem GroupID.

Enable IsTrigger of the Collider
It disables the physical contact, and hence it's easier to handle.

Disable UseGravity and enable IsKinematic of the rigidbody
It disables free fall and fixes the item on the air which is easy to handle.

VCI script

main.lua
-- Search for "Target" and store it in _Target
local _Target = vci.assets.GetSubItem("Target")
 
function onUse(use)
    -- If the name of the item used was "Switch"
    if use == "Switch" then
 
        -- Enlarge it if it is minimized
        if _Target.GetLocalScale().x < 0.1 then
            local scale = Vector3.__new(1, 1, 1)
            _Target.SetLocalScale(scale)
        end
 
        -- minimize it if it is enlarged
        if _Target.GetLocalScale().x > 0.1 then
            local scale = Vector3.__new(0.001, 0.001, 0.001)
            _Target.SetLocalScale(scale)
        end
 
    end
end

VCI Script (Description)

local _Target = vci.assets.GetSubItem(“Target”)
It searches for a SubItem with the name “Target” and stores the information of the SubItem into the variable “_Target”.
You can get the information on the SubItem of the Target by accessing _Target.

_Target.GetLocalScale().x
You can access the x scale of the Target.
This value is 0.001 when it is minimized (or hidden), 1 when enlarged (or shown).
Therefore, by comparing this value, you can tell if it is enlarged or minimized.

local scale = Vector3._new(1, 1, 1)
_Target.SetLocalScale(scale)
The scale of the item is described with three values (Vector3): x, y and z.
Create a scale of your preference with Vector3.__new() and apply it with _Target.SetLocalScale().

The item doesn't actually disappear, but it seems to have as it is invisibly small.
The issue with this approach is that the collision detection is still there, and it monopolizes the scale value for appear/disappear states.
You can achieve a similar effect by moving the object to a position far away.
Choose between the scale approach or the position approach, depending on what you want to make.

en/vci/sample/transform/no3.txt · Last modified: 2023/08/28 20:20 by pastatto

Page Tools