Lerping Error for Gun CFrame

i’ve just got into learning :Lerp(),i have tryed re-creating this below

https://gyazo.com/9bed9be6fd0ee7bd2a9f0463b1831def

local tool = script.Parent

tool.Equipped:Connect(function()
  for i = .1,1,.1 do
    workspace.Camera.CFrame = workspace.Camera.CFrame:Lerp(workspace.Camera.CFrame * CFrame.new(0,0,2), 0.05)
 end
end)

Please read scripting support guidelines. I don’t know what your problem is because you haven’t explained it.

2 Likes

Basically my problem is that when the gun equips,its suppose to lerp cameras position like in the example below,No Errors in output

That doesn’t tell me what the problem is still. Looking at your code I can see you’re using a for loop without any yielding. The code in the for loop is going to execute instantly.

If you want to Lerp the camera you need to use RunService.RenderStepped in a LocalScript. You can use RunService.RenderStepped:Wait() inside of the for loop and it will wait for one frame. Each frame the camera is rendered so that will sync the movement with each frame.

1 Like

Lovely ill try it,ill get back to you once i experiment with it

Alright. Make sure you read the Scripting Support guidelines too.

1 Like

can i just insert RunService.RenderStepped:Wait() above the lerping code like this

local tool = script.Parent
local RunService = game:GetService("RunService")
tool.Equipped:Connect(function()
  for i = .1,1,.1 do
	RunService.RenderStepped:Wait(1)
    workspace.Camera.CFrame = workspace.Camera.CFrame:Lerp(workspace.Camera.CFrame * CFrame.new(0,0,2), 0.05)
 end
end)

i tryed this

local tool = script.Parent
local Camera = game.Workspace.Camera
tool.Equipped:Connect(function()
local Orig = workspace.Camera.CFrame
  for i = 0,1,.1 do
	Camera.CameraType = Enum.CameraType.Scriptable
   Camera.CFrame = Orig:Lerp(Orig * CFrame.new(0, 0, 20), i)
 wait(0.01)
 print(1)
 end
end)

Event:Wait() doesn’t have any function arguments. It just waits for the event to fire. Event:Connect() calls a function every time the event fires. RenderStepped is an event on the service RunService which fires every frame.

You can insert it into the for loop and it will wait for one frame each iteration of the loop. The game runs at 60 fps normally so Lerping for 60 frames will Lerp for about one second.

Lerp’s second argument (delta) is like a percent. It takes the CFrame you give it in arguments (the target) and moves the CFrame before the : to the target CFrame. If delta is one the value Lerp returns will be the target. If delta is zero the value Lerp returns is the original CFrame.

So your delta should be i/maxi. So if your for loop goes from 1 to 60 and you wait a frame at the beginning your delta should be i/60. You can also put the 60 in a variable to make it easy to change. You can also use something like this to specify seconds to lerp: timeToLerp*60

2 Likes

i would think this would be the time of the lerp

CFrame.new(0, 0, 2), i,60)

the 60 ^

Lerp doesn’t take a time input. Lerp takes a decimal value as input.

For example:
cf1:Lerp(cf2, 0.5)
This will return a value half way in between cf1 and cf2.

1 Like

i got it to work

local tool = script.Parent
local Camera = game.Workspace.CurrentCamera
tool.Equipped:Connect(function()
local Orig = workspace.CurrentCamera.CFrame
  for i = 0,1,.1 do
	Camera.CameraType = Enum.CameraType.Scriptable
   Camera.CFrame = Orig:Lerp(Orig * CFrame.new(0, 0, 2), i)
 wait(0.01)
 print(1)
 end
end)

Thank you so much for your help!,i forgot to set Require Handle to false,since i was just testing