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

How do I make this more accurate?

Asked by
theCJarmy7 1293 Moderation Voter
8 years ago
Edited 6 years ago
01function tween(part,pos,tim,stop) --stuff
02    local function stuff() --for the stopige
03        local runs = 100 --smoooth
04        local ToTake = runs*.03 --time it will take
05        if ToTake>tim then
06            repeat
07                runs=runs-1
08                ToTake = runs*.03
09            until ToTake<tim --gets closer to tim
10        else
11            repeat
12                runs=runs+1
13                ToTake = runs*.03
14            until ToTake>tim
15        end
View all 28 lines...

The above is a moduleScript. The purpose of it is to smoooooothly move parts. It works fine, but if I do something like this.

1p1 = game.Workspace:WaitForChild("Part")
2p2 = game.Workspace:WaitForChild("Part2")
3tween = require(game.ReplicatedStorage.tweens)
4 
5start = tick()
6tween(p1,p2.CFrame,10,true) --it has to pause here
7print(tick()-start) --print 11.3

So, does anybody know how I could improve the accuracy of the time?

1 answer

Log in to vote
3
Answered by
EgoMoose 802 Moderation Voter
8 years ago

You have the right idea with the lerp function. The goal here is to get a value between 0 and 1 and have it correlate to the time it should take for the entire tween to complete.

In my opinion the easiest way to do this is to get the tick when the tween is started and then find the difference in time after you wait or do a renderstep, etc.

So for example:

01function tween(part, goal, finalt)
02    local origin = part.CFrame; -- where we start from
03    local start = tick(); -- the initial tick we compare to
04    local dt = 0; -- the change in time from initial tick until now
05    while dt < finalt do
06        dt = tick() - start; -- update out time change
07        part.CFrame = origin:lerp(goal, dt/finalt); -- get as percentage
08        wait(); -- or renderstepped, etc...
09    end;
10    part.CFrame = goal; -- set it at the end to be precise
11end;
12 
13-- example
14local s = tick();
15tween(game.Workspace.Part, CFrame.new(), 3);
16print(tick() - s);
2
Oh, thank you. I never thought about using tick() in a function before. Also, why the semi colons? theCJarmy7 1293 — 8y
3
Habit from other programming languages EgoMoose 802 — 8y
Ad

Answer this question