How could I use CFrame:Lerp() here?

I’ve tried looking for tutorials but I couldnt find anything

		local movingOffset = CFrame.new(0,0,( math.clamp( Magnitude / Humanoid.WalkSpeed ,-.2 , .2 )))	
		ViewModelRoot.CFrame = CF * movingOffset * AIMCFRAME   *  (CustomViewModelPosition)

the cframe is movingOffset; i’d like it so it doesnt instantly teleport to there and it lerps in 0.5 seconds for example

thanks for responses in advance

1 Like

bump, still stuck on this
ive made the cframes move, so it just needs smoothening now

Try lerping 10 (or any amount) times (with a task.wait() or whaterever) 0.5 / the amount in the for loop

1 Like

Its on a renderstepped so i dont think i can put a wait, unless im wrong
i dont understand lerping

RunService.RenderStepped:Wait()? then

1 Like

wont that halt the whole renderstepped? i have alot of other things in there that cant wait for a lerp

Wait but then, cant you like lerp 0.01 or so? since its on renderstepped and its basically gonna handle it like a for loop or whaterever (tell me if im wrong i dont know how renderstepped works)

1 Like

I have no idea too but i tried putting 0.1 on it and it seems to just do 1/10 of the entire cframe, no lerping

Try addind a “lerpvalue” variable or something and set it to (value % 1) + lerp amount (0.1 for you) every frame

1 Like

sorry, what does value % 1 mean?

I dont know actually..
but it does set the lerp value or whaterever the variable name is to 0 after it reaches 1

okay so it actually returns the remainder of the division
so if 2 / 2 = 1 then 2 % 2 = 0

that means if the lerping value is equal to 1, value % 1 will return 0

1 Like

i dont really understand, sorry
i dont know what to do with that, i dont understand lerp

Do :Lerp(the cframe, lerp value)

1 Like

You can use the simpler math.lerp here since that’s the only part that actually changes.

Using zValue as math.clamp( Magnitude / Humanoid.WalkSpeed ,-.2 , .2 ),

  1. Store the time that the movement begins (I’ll refer to as startTime).
  2. Calculate the difference in time and divide it by how long the action should take (I’ll refer to as t). t = (time() - startTime) * 2, note I used * 2 as the inverse of / 0.5.
  3. Clamp t from -1 to 1.
  4. Lerp from 0 to your target Z over t. math.lerp(0, zValue, t)
  5. Create a CFrame using the output of the lerp and perform the same steps as normal.
  6. (Optionally) When movement ends, invert the t calculation so it approaches -1.

Note, this solution may be an answer to an XY problem. You seem to be wanting a way to simulate inertia. A better solution would be to simulate physics. There’s many approaches to that one; I would suggest calculating an acceleration value to add to a calculated velocity which is added to the current position to reach a target position (and then add a PID controller along here to make it nicer). You’d modify the target position in response to where you want it to go.

1 Like

Hi, thanks for responding, ill try this out soon

Whats a PID controller?

I notice you are determining the Z-offset straight from the Magnitude. This causes the “snapping” issue because as soon as you stop moving, Magnitude hits zero and the gun instantly teleports.

Instead of Lerping the full CFrame, you should stick to math.lerp to smooth that Z value.

1 Like

Hey I tried this, i may be doing something wrong but it made everything stay still

i have this running in a renderstepped so that might be affecting it

also i dont know whether to define the time variables inside or out the renderstepped

this is in a renderstepped loop (i tried defining the variables outside aswell)

local startTime = time()
		local t = math.clamp( (time() - startTime)  * 2, -1,1 )
		local zvalue = math.lerp(0, -0.0494765043258667 / math.clamp(AimFactor,1,5) * moveDirectionX,t )
		local cvvalue = math.lerp(0 ,math.clamp( ( (Magnitude / Humanoid.WalkSpeed) ) / math.clamp(AimFactor,1,5) ,0 , .2 ) ,t)
		local movingOffset = CFrame.Angles(0,0, zvalue  ) *  CFrame.new(  ( moveDirection.X  * .05 ) / math.clamp(AimFactor,1,5)  , 0 , cvvalue)
		
		ViewModelRoot.CFrame = CF * movingOffset * AIMCFRAME   *  (CustomViewModelPosition)

this is everything im trying to lerp

local movingOffset = CFrame.new():Lerp( CFrame.Angles(0,0, ( (-0.0494765043258667 / math.clamp(AimFactor,1,5)) * moveDirectionX)   ) *  CFrame.new(  ( moveDirection.X  * .05 ) / math.clamp(AimFactor,1,5)  , 0 , math.clamp( ( (Magnitude / Humanoid.WalkSpeed) ) / math.clamp(AimFactor,1,5) ,0 , .2 )   ), 1 )

hey, I tried putting this in and its the closet so far

indexm = indexm%1 + .01

it lerps then resets; is there anyway to make it procedurally lerp? so it’ll naturally lerp if its moved from where it currently is to the new position

Try checking if the value is BELOW 1 then add .01, else set it to 1? that should do the trick

1 Like

i tried

		indexm = indexm%1
		
		if indexm < 1 then
			indexm += .01
		end

and

		if indexAim <1 then
			indexm = indexm%1 + .01
		end	

Both ending up doing the same thing,
should i define index%1 outside the renderstepped or anything? i have it defined as indexm = 0 outside the loop, then the code above inside the renderstepped