How would I smoothly lerp back these .C0's?

Hello, I’m making guns right now and I just implemented a script that gets your arms and head to move with the camera. I’m using a renderstepped function and save the original default C0 values before it starts, and then set the player’s C0s back to the default values once the tool is unequipped. I just don’t know how to smoothly transition or lerp the C0s back to the default value without it overwriting because of the function

Here’s the script.

plr = game.Players.LocalPlayer;
repeat
    wait()
until plr.Character
char = plr.Character
m = plr:GetMouse()
local IsEquipped = false

local origRightS = char:WaitForChild("Torso"):WaitForChild("Right Shoulder").C0
local origLeftS = char:WaitForChild("Torso"):WaitForChild("Left Shoulder").C0
local origNeck = char:WaitForChild("Torso"):WaitForChild("Neck").C0

local c = game.Workspace.CurrentCamera

game:GetService("RunService").RenderStepped:Connect(function()
    if IsEquipped then
        char.Torso["Right Shoulder"].C0 = CFrame.new(1, 0.5, 0) * CFrame.Angles(-math.asin((m.Origin.p - m.Hit.p).unit.y), 1.55, 0)
        char.Torso["Left Shoulder"].C0 = CFrame.new(-1, 0.5, 0) * CFrame.Angles(-math.asin((m.Origin.p - m.Hit.p).unit.y), -1.55, 0)
        char.Torso["Neck"].C0 = CFrame.new(0, 1, 0) * CFrame.Angles(-math.asin((m.Origin.p - m.Hit.p).unit.y) + 1.55, 3.15, 0)
    else  -- Here is where the lerping/tweening should happen, it sets the C0's back to the default ones
        char.Torso["Right Shoulder"].C0 = origRightS  
        char.Torso["Left Shoulder"].C0 = origLeftS
        char.Torso["Neck"].C0 = origNeck
    end
end)


script.Parent.Equipped:Connect(function()
    IsEquipped = true
end)

script.Parent.Unequipped:Connect(function()
    IsEquipped = false
end)
1 Like

You’ll have to tweak the delta of the lerp (linear interpolation) if you want it any slower/faster after it’s defined by RenderStepped:

plr = game.Players.LocalPlayer;
repeat
    wait()
until plr.Character
char = plr.Character
m = plr:GetMouse()
local IsEquipped = false

local origRightS = char:WaitForChild("Torso"):WaitForChild("Right Shoulder").C0
local origLeftS = char:WaitForChild("Torso"):WaitForChild("Left Shoulder").C0
local origNeck = char:WaitForChild("Torso"):WaitForChild("Neck").C0

local c = game.Workspace.CurrentCamera

game:GetService("RunService").RenderStepped:Connect(function(delta)
    if IsEquipped then
        char.Torso["Right Shoulder"].C0 = CFrame.new(1, 0.5, 0) * CFrame.Angles(-math.asin((m.Origin.p - m.Hit.p).unit.y), 1.55, 0)
        char.Torso["Left Shoulder"].C0 = CFrame.new(-1, 0.5, 0) * CFrame.Angles(-math.asin((m.Origin.p - m.Hit.p).unit.y), -1.55, 0)
        char.Torso["Neck"].C0 = CFrame.new(0, 1, 0) * CFrame.Angles(-math.asin((m.Origin.p - m.Hit.p).unit.y) + 1.55, 3.15, 0)
    else  -- Here is where the lerping/tweening should happen, it sets the C0's back to the default ones
        char.Torso["Right Shoulder"].C0 = char.Torso["Right Shoulder"].C0:lerp(origRightS, delta)
        char.Torso["Left Shoulder"].C0 = char.Torso["Left Shoulder"].C0:lerp(origLeftS, delta)
        char.Torso["Neck"].C0 = char.Torso["Neck"].C0:lerp(origNeck, delta)
    end
end)


script.Parent.Equipped:Connect(function()
    IsEquipped = true
end)

script.Parent.Unequipped:Connect(function()
    IsEquipped = false
end)
2 Likes

Oh alright, I’ll try this. What I tried before was setting IsEquipped false and then doing a for loop for the lerping but it didn’t work

Edit: Worked perfectly, thanks!

1 Like