-
What do you want to achieve?
I have created a head bobbing, sprinting and crouching character local script for a game I am working on. -
What is the issue?
When I look down, while crouching in first person, the character slowly moves forward. I am also unable to move backwards or to the side at all when crouching in first person. -
What solutions have you tried so far?
I’ve attempted to see any character movement, but there is nothing on it. My idea is that the character is somehow following the camera offset in first person, moving it. I have no idea how to turn that off.
The code below is a localcharacterscript and controls the camera. The remoteevents connect to a serverside that controls how fast the player is going. The Lightpart controls a flashlight and works fine, but is still part of the script.
local runService = game:GetService("RunService")
local userInputService = game:GetService("UserInputService")
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
RegOffset = Vector3.new(0,0,0)
local Crouching = false
local Running = false
Extremity = .6
TimeTo = 2.5
function updateBobbleEffect()
local currentTime = tick()
local bobble = Vector3.new(0,0,0)
if humanoid.MoveDirection.Magnitude > 0 then -- we are walking
local bobbleX = math.cos(currentTime * TimeTo) * Extremity
local bobbleY = math.abs(math.sin(currentTime * TimeTo)) * Extremity
local bobble = Vector3.new(bobbleX, bobbleY, 0) + RegOffset
humanoid.CameraOffset = humanoid.CameraOffset:lerp(bobble, 0.25)
else -- we are not walking
humanoid.CameraOffset = humanoid.CameraOffset:lerp(RegOffset, 0.05)
end
game.Workspace.LightPart.CFrame = game.Workspace.LightPart.CFrame:Lerp(game.Workspace.CurrentCamera.CFrame - bobble, .1)
end
function Key(KeyHit)
print("key")
if KeyHit.KeyCode == Enum.KeyCode.LeftControl or KeyHit.KeyCode == Enum.KeyCode.ButtonL1 then
print("crouch")
if Crouching == true then
Crouching = false
Running = false
RegOffset = Vector3.new(0,0,0)
Extremity = .6
TimeTo = 5
script.Parent.CrouchRemote:FireServer(false)
else
Crouching = true
RegOffset = Vector3.new(0,-2,0)
Extremity = .3
TimeTo = 2.5
script.Parent.CrouchRemote:FireServer(true)
end
elseif KeyHit.KeyCode == Enum.KeyCode.LeftShift or KeyHit.KeyCode == Enum.KeyCode.ButtonR1 then
print("run started")
Running = true
Crouching = false
RegOffset = Vector3.new(0,-0.3, 0)
Extremity = 1
TimeTo = 7
script.Parent.RunRemote:FireServer(true)
end
end
function EndKey(KeyHit)
if KeyHit.KeyCode == Enum.KeyCode.LeftShift or KeyHit.KeyCode == Enum.KeyCode.ButtonR1 then
print("run ended")
Running = false
RegOffset = Vector3.new(0,0,0)
Extremity = .3
TimeTo = 2.5
script.Parent.RunRemote:FireServer(false)
end
end
runService.RenderStepped:Connect(updateBobbleEffect)
userInputService.InputBegan:Connect(Key)
userInputService.InputEnded:Connect(EndKey)