The common way this is dealt with is through springs. This allow the game coder to set some values regarding how the spring moves and then update the target. This allows you to change the spring target dynamically and not have to worry about the for loop timing or other animations.
There's a wiki article on this topic, that gives a broad overview of the concept that can be read here, but to really understand how I got to the code I'm about to post you'll need to do some calculus.
03 | local spring_mt = { __index = spring } ; |
05 | function spring.new(position, velocity, zeta, omega) |
07 | self.position = position; |
08 | self.velocity = velocity; |
11 | self.target = position; |
12 | return setmetatable (self, spring_mt); |
15 | function spring:update(dt) |
16 | if (self.zeta > = 1 ) then |
20 | if (self.zeta < 0 ) then |
24 | local x 0 = self.position - self.target; |
25 | local omegaZeta = self.omega*self.zeta; |
26 | local alpha = self.omega*math.sqrt( 1 -self.zeta*self.zeta); |
27 | local exp = math.exp(-dt*omegaZeta); |
28 | local cos = math.cos(dt*alpha); |
29 | local sin = math.sin(dt*alpha); |
30 | local c 2 = (self.velocity+x 0 *omegaZeta) / alpha; |
32 | self.position = self.target + exp*(x 0 *cos + c 2 *sin); |
33 | self.velocity = -exp*((x 0 *omegaZeta - c 2 *alpha)*cos + (x 0 *alpha + c 2 *omegaZeta)*sin); |
With that we can easily set targets and update them properly with a time step.
Here's a very simple example wherein I use the spring to bring the angle back down, but potentially you also want to use it to go up too.
01 | local mouse = game.Players.LocalPlayer:GetMouse(); |
02 | local camera = game.Workspace.CurrentCamera; |
03 | local part = game.Workspace.Part; |
04 | local spring = require(game.Workspace.Spring); |
06 | local s = spring.new( 0 , 0 , 0.6 , 10 ); |
07 | mouse.Button 1 Down:connect( function () |
11 | game:GetService( "RunService" ).RenderStepped:connect( function (dt) |
13 | part.CFrame = camera.CFrame * CFrame.Angles(math.rad(s.position), 0 , 0 ) * CFrame.new( 1 , - 1 , - 3 ); |
Hopefully you get the jist!