So, I’ve made this code where it basically waits for player input (A or D) and leans the camera in function of the key, but the thing is that It doesn’t lean the camera, instead if moves my entire character (when pressing shift lock).
And I would like to make it so that you lean just like in real life. Here the code:
-- Get services
local uis = game:GetService("UserInputService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
-- Variables
local Player = Players.LocalPlayer
Player.CharacterAdded:Wait()
local Character = Player.Character
local hum = Character:WaitForChild("Humanoid")
local camera = game.Workspace.CurrentCamera
local camCframeRotation = game.Workspace.CurrentCamera.CFrame.Rotation
-- Functions
-- Left
function leftCameraLeaning()
local startCframe = camera.CFrame
local leftTargetCframe = camera.CFrame * CFrame.fromEulerAnglesXYZ(0, -45, 0)
local lerptime = 3
local startTime = tick()
local runningTime = 0
local lerp
lerp = RunService.Heartbeat:Connect(function(deltaTime)
runningTime += deltaTime
local alpha = runningTime / lerptime
camera.CFrame = startCframe:Lerp(leftTargetCframe, alpha)
if alpha >= 1 then
lerp:Disconnect()
end
end)
end
-- Right
function rightCameraLeaning()
local startCframe = camera.CFrame
local rightTargetCframe = camera.CFrame * CFrame.fromEulerAnglesXYZ(0, -45, 0)
local lerptime = 3
local startTime = tick()
local runningTime = 0
local lerp
lerp = RunService.Heartbeat:Connect(function(deltaTime)
runningTime += deltaTime
local alpha = runningTime / lerptime
camera.CFrame = startCframe:Lerp(rightTargetCframe, alpha)
if alpha >= 1 then
lerp:Disconnect()
end
end)
end
-- Tables
local Keys = {
[Enum.KeyCode.W] = _,
[Enum.KeyCode.S] = _,
[Enum.KeyCode.A] = leftCameraLeaning,
[Enum.KeyCode.D] = rightCameraLeaning
}
-- Input
uis.InputBegan:Connect(function(input, isTyping)
local KeyCode = input.KeyCode
if Keys[KeyCode] and not isTyping then
Keys[KeyCode]()
end
end)
And I have no clue on how to stop the lerping when input ended