en:vci:sample:lerpmotion

Point A to B movement using lerp

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.

Example data

Unitypackage

tutorial_lerp.unitypackage

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: 2023/09/29 20:45 by pastatto

Page Tools