I am trying to make a crawling script that makes the camera go down with the player when the player toggles crawling, but in my crawl function the player.Character.Humanoid.CameraOffset = Vector3.new(0,-5,0) is only making the camera go down for like 3 frames then it goes back up. The only other camera affecting script in my game is a script that adds camera sway, and even when I delete that script the offset still doesn’t work properly. Do I need to make a custom camera system for this to work?
Code:
local player = game.Players.LocalPlayer
repeat wait() until player.Character
local UserInputService = game:GetService("UserInputService")
local camera = game:GetService("Workspace").CurrentCamera
local AnimationId = "282574440"
local Anim = Instance.new("Animation")
Anim.AnimationId = "rbxassetid://" .. AnimationId
local k = player.Character.Humanoid:LoadAnimation(Anim)
k:AdjustSpeed(1)
local isCrawling = false
local originalWalkSpeed = player.Character.Humanoid.WalkSpeed -- Store the original walk speed
local function toggleCrawling()
if isCrawling then
k:Stop()
player.Character.Humanoid.WalkSpeed = originalWalkSpeed
else
k:Play()
player.Character.Humanoid.WalkSpeed = originalWalkSpeed / 2
player.Character.Humanoid.CameraOffset = Vector3.new(0,-5,0) --make it so the camera stays there 5 studs below the head 0, -5, 0
camera.FieldOfView = 60 --make it so the camera is zoomed in when crawling
end
isCrawling = not isCrawling
end
local function checkKeys(input)
if input.KeyCode == Enum.KeyCode.C or
input.KeyCode == Enum.KeyCode.LeftControl then
toggleCrawling()
end
end
UserInputService.InputBegan:Connect(checkKeys)
local player = game.Players.LocalPlayer
repeat wait() until player.Character
local UserInputService = game:GetService("UserInputService")
local isRunning = false
local originalWalkSpeed = player.Character.Humanoid.WalkSpeed -- Store the original walk speed
local function toggleRunning()
if isRunning then
player.Character.Humanoid.WalkSpeed = originalWalkSpeed -- Restore the original walk speed
else
player.Character.Humanoid.WalkSpeed = 20 -- Reduce walk speed while crawling
end
isRunning = not isRunning
end
local function checkKeys(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
toggleRunning()
end
end
UserInputService.InputBegan:Connect(checkKeys)
(Issue is in the local function toggleCrawling() line 22 of the script)
So when I originally tested it and pressed C, the camera would go down and stay down, but it would not go back up if I pressed it again. There were a lot of other issues with the script as well, so I decided to quickly fix it up.
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local UserInputService = game:GetService("UserInputService")
local Anim = Instance.new("Animation")
local AnimationId = "282574440"
Anim.AnimationId = "rbxassetid://" .. AnimationId
local crawlAnimation = player.Character.Humanoid:LoadAnimation(Anim)
crawlAnimation:AdjustSpeed(1)
local defaultWalkSpeed = player.Character.Humanoid.WalkSpeed -- Original walk speed
local isCrawling = false
local function toggleCrawling()
local crawlingOffset = Vector3.new(0,-5,0)
if isCrawling then
crawlAnimation:Stop()
player.Character.Humanoid.WalkSpeed = defaultWalkSpeed
player.Character.Humanoid.CameraOffset = Vector3.zero
else
crawlAnimation:Play()
player.Character.Humanoid.WalkSpeed = defaultWalkSpeed / 2
player.Character.Humanoid.CameraOffset = crawlingOffset
end
isCrawling = not isCrawling
end
local runSpeed = 20 -- this is the character run speed
local isRunning = false
local function toggleRunning()
if not isCrawling then
if isRunning then
player.Character.Humanoid.WalkSpeed = defaultWalkSpeed
else
player.Character.Humanoid.WalkSpeed = runSpeed
end
isRunning = not isRunning
end
end
local function inputBegan(input, processed)
if not processed then -- if player is not typing in chat
if input.KeyCode == Enum.KeyCode.C then
toggleCrawling()
end
if input.KeyCode == Enum.KeyCode.LeftShift then
toggleRunning()
end
end
end
function inputEnded(input, processed)
if not processed then
if input.KeyCode == Enum.KeyCode.LeftShift then
toggleRunning()
end
end
end
UserInputService.InputBegan:Connect(inputBegan)
UserInputService.InputEnded:Connect(inputEnded)
It worked for me, so if you continue to have the issue you described with this script above ^, then it likely has to do with another script/something else in your game.
Yeah it was another script in my game interfering with my camera offset (it was my camera sway script) so I had to update that to use camera.CFrame instead. Also I wanted to say thanks for fixing another problem I actually had because when I was crawling the sprint script wasn’t deactivating and the result was the player having the crawl animation while moving fast.