Prevent character from looking backwards

I’m using a camera bobble to give a better sprint effect. However, when you look down, the bobble effect sometimes causes the camera angle to be closer to the back than to the front, which results in the character looking backwards.

(Video showing the issue): https://gyazo.com/c5c6dde04e8126c60720331e9795b56f

I know that I can fix this with the Humanoid's property AutoRotate, but the character will never face the direction you’re walking to.

Is there any way to prevent this from happening? Or can I somehow stop the camera from moving at a certain angle?

can you post your script? it’s hard to help when I don’t know what you wrote in your script

I’m not gonna post the entire script because it’s too long, but there are the relevant parts of it:

local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local Player = game:GetService("Players").LocalPlayer
local Camera = workspace.CurrentCamera
local Character = script.Parent
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Humanoid = Character:WaitForChild("Humanoid")
local Head = Character:WaitForChild("Head")
local Animator = Humanoid:WaitForChild("Animator")


local RotationFrequency, RotationIntensity = 3.6, 0.35
local VerFrequency, VerIntensity = 9.5, 0.15
local DefWalkSpeed = 24

local DefaultSpeed = 12
local MaxSpeed = 32
local MinSpeed = 6

local DefaultFOV = 70
local MaxFOV = 90

local PreviousVelocity = nil
local CurrentTime = nil

local function Lerp(a, b, Time)
	return (a + (b - a) * Time :: number)
end

local function GetCurve(Frequency, Intensity)
	return (math.sin(os.clock() * Frequency) * Intensity :: number)
end


RunService.RenderStepped:Connect(function()
	local Velocity = math.round(Vector3.new(HumanoidRootPart.AssemblyLinearVelocity.X, HumanoidRootPart.AssemblyLinearVelocity.Y, HumanoidRootPart.AssemblyLinearVelocity.Z).Magnitude)

	if PreviousVelocity then
		Velocity = Lerp(PreviousVelocity, Velocity, 0.25)
	end

	if Humanoid:GetState() == Enum.HumanoidStateType.Running then
		-- Camera.CFrame = Camera.CFrame * CFrame.new(0, GetCurve(VerFrequency, VerIntensity) * Velocity / DefWalkSpeed, 0) * CFrame.Angles(0, 0, math.rad(GetCurve(RotationFrequency, RotationIntensity) * Velocity / DefWalkSpeed))
		Camera.CFrame = Camera.CFrame * CFrame.new(0, GetCurve(VerFrequency, VerIntensity) * Velocity / DefaultSpeed, 0) * CFrame.Angles(0, 0, math.rad(GetCurve(RotationFrequency, RotationIntensity) * Velocity / DefaultSpeed))
	end
end)

you made it more difficult by trying to change the camera’s CFrame instead change the camera offset property in the humanoid here is a simple camera bobbing script i wrote i while ago:

local RunService = game:GetService("RunService")
local plr = game:GetService("Players").LocalPlayer

RunService.RenderStepped:Connect(function(dt)
	local CurrentTick = tick()
	local Character = plr.Character
	if not Character then
		return
	end
	if Character:FindFirstChild("Humanoid") then
		if Character.Humanoid.MoveDirection.Magnitude > 0 then
			local BobbleY = math.cos(CurrentTick * Character.Humanoid.WalkSpeed) * 0.5
			local Bobble = Vector3.new(0, BobbleY, 0)
			Character.Humanoid.CameraOffset = Character.Humanoid.CameraOffset:lerp(Bobble, .25)
		else
			Character.Humanoid.CameraOffset = Character.Humanoid.CameraOffset:lerp(Vector3.new(0,0,0), .25)
		end
	end
end)

The problem is that your script changes the camera offset, and it’s relative to the character. In my script the arms of the character are visible, and if I change the camera offset the arms will start moving a lot

Thank you for trying to help though

one more solution is to check the camera angle if the x orientation of the camera is less than -70 deg then disable your bobbing effect

That makes sense. It’s not very smooth but I’ll keep working on it. I will mark your reply as solution for now

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.