Translations of this page:

Sidebar

en:vci:sample:lerpmotion

Point A to B movement using lerp

Example data:
https://seed.online/items/9ca60bffc7da3a60b32b4cf0c7c55ad6db598199aec584d646b234a31aed396a

UnityPackage: tutorial_lerp.zip

In this example, an object will move from point A to point B in N seconds, using interpolation.
Let's use the point where the item is used as point A. It moves to the point B (0,1,0) in 4 seconds.

main.lua
local framevalue = vci.me.Time.TotalSeconds
local framestart = 0
 
local sub = vci.assets.GetTransform("Sub")
 
local pointA = Vector3.__new(0,1,0)--Point A
local pointB = Vector3.__new(0,1,0)--Point B
 
local pointnow =Vector3.zero
 
local moveflag = 0
local distance = 0.01 --Stops when the distance is shorter than this
 
function updateAll()
    if moveflag==1 then
        framevalue = (vci.me.Time.TotalSeconds-framestart)/4 --Takes 4 seconds to move
        --Move from pointA to pointB in N seconds, using interpolation
        pointnow = Vector3.Lerp(pointA,pointB,framevalue)
        sub.SetLocalPosition(pointnow)
 
        if Vector3.Magnitude(sub.GetLocalPosition()-pointB)<distance then
            moveflag=0
        end
    end
end
 
 
function onUse(use)
    --Set the position where onUse is called as the point A
    framestart = vci.me.Time.TotalSeconds
    pointA = sub.GetLocalPosition()
    moveflag = 1
end
en/vci/sample/lerpmotion.txt · Last modified: 2022/03/06 23:14 by Ramen

Page Tools