Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Can lerped positions be set directly?

Asked by 7 years ago
Edited 7 years ago

This script is designed to lerp the position of a part between two target locations; where plat is the part, pos1 is the first position, pos2 is the second position, dur is the duration of the movement from one location to the next and back, and NUMBER_OF_BETWEENS is the amount of positions shown in between the target positions.

local plat=script.Parent--Part 
local pos1=plat.Value1.Value--Target Position 1
local pos2=plat.Value2.Value--Target Position 2
local dur=plat.Dur.Value--Total duration of one loop
plat.Position=pos1--Set the part position to the first value
local NUMBER_OF_BETWEENS=100--Amount of "in between" positions

while true do
    wait(dur/(NUMBER_OF_BETWEENS*2))--Wait for an amount that goes with the duration
    for i=0,1,(1/NUMBER_OF_BETWEENS) do--loop i from 0 to 1
        plat.Position=pos1:Lerp(pos2,i)--This SHOULD set the part position to the lerped amount
    end
    for i=0,1,(1/NUMBER_OF_BETWEENS) do
        plat.Position=pos2:Lerp(pos1,i)--same as before but the opposite direction
    end
end

When the script is run, the part is set to the first value, and the loop runs constantly, BUT, the part position is not updated as the loop runs. I do not see any bugs, but I am wondering if I am missing some concept.

Is there a minimum wait() duration? I don't know whether this would affect it but that's why I'm asking

Can the part position not be set to the lerped position?

Thank you for any responses! :)

1 answer

Log in to vote
0
Answered by
RubenKan 3615 Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

You don't have a wait() inside your for loops.

I added a wait() inside of your two for loops, so you can actualy see it (the part 'plat') lerp instead of being back at the starting position instantly after every loop.

Also i removed your wait(dur/(NUMBER_OF_BETWEENS*2)) because that would just delay your loop, and i suppose you don't actualy want that.

Result:

while true do
    for i=0,1,(1/NUMBER_OF_BETWEENS) do
        wait()
        plat.Position=pos1:Lerp(pos2,i)
    end
    for i=0,1,(1/NUMBER_OF_BETWEENS) do
        wait()
        plat.Position=pos2:Lerp(pos1,i)
    end
end
0
This makes sense, I'll try it out. I think I just had the wait in the wrong place. The wait in each loop would be the dur/NUMBER_OF_BETWEENS*2 so that it loops took a controllable amount of time to execute rather than executing as fast as possible. ilikehobbits1 2 — 7y
0
It worked. Thanks! ilikehobbits1 2 — 7y
0
No problem! Glad i could help you :D RubenKan 3615 — 7y
Ad

Answer this question