LoginSignup
2
1

More than 3 years have passed since last update.

VCIで親子関係っぽい振る舞いをさせてみる(位置だけ……

Last updated at Posted at 2019-12-02

概要

VCIで親子関係使えないの不便ですよね。ということで位置だけですが、親子関係っぽい振る舞いをさせてみました。

動作

実装

image.png
image.png※Cube1~3は同じ

local current = ""
local relation = {"Cube1", "Cube2", "Cube3"}
local localPos = {Vector3.zero, Vector3.zero}
local index = 1

function updateAll()
    SetPos()
end

function SetPos()
    if current == relation[index] then
        -- 子の位置を移動
        for i = index, #relation - 1 do
            local pos = vci.assets.GetSubItem(relation[i]).GetPosition()
            vci.assets.GetSubItem(relation[i + 1]).SetPosition(pos - localPos[i])
        end
    else
        -- 子の位置を更新
        local parent = vci.assets.GetSubItem(relation[index]).GetPosition()
        local child = vci.assets.GetSubItem(relation[index + 1]).GetPosition()
        localPos[index] = parent - child

        if index < #relation - 1 then
            -- 次の子へ
            index = index + 1
            SetPos()
        else
            index = 1
        end
    end
end

function onGrab(target)
    current = target
end

function onUngrab(target)
    current = ""
end

捕捉

currentは掴んでいるオブジェクトの名前が入るので、掴んでいるオブジェクトの子に対して、位置を移動するか、ローカル座標?の更新をします。relationには先頭から親~子の順で名前を入れておきます。localPosは子になるオブジェクトのローカル座標?を入れます。indexはSetPos関数が再帰なので、relation参照時の要素数として使います。

蛇足

補足の文中のローカル座標に?が付いている辺りでお察し頂ければと思いますが、自信がないので、「こいつ頭悪いなー」程度に眺めて笑ってもらえたらと思います。きっとカッコいい式でもっとスマートに実現できる気がしています。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1