How to get body to follow mouse only looking forward

I want the the character to only face forward and not backward while aiming with the mouse.
Whenever I point the mouse behind me character (or below it), he turns around.


After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
I’ve tried using

math.clamp()

but I don’t think I used it properly.

Here’s the piece of script for the mouse

MouseMoveFunction = Mouse.move:Connect(function()
			local direction = ((Mouse.Hit.p - character.HumanoidRootPart.Position) * Vector3.new(1, 0, 1))
			print(Mouse.Target)
			character.HumanoidRootPart.CFrame = CFrame.new(character.HumanoidRootPart.Position, character.HumanoidRootPart.Position + direction)
		end)

I think there might be something wrong with your mouse.

Nothing wrong with the mouse, the mouse p use every coordinates the mouse can hit

So should I not use mouse p for this one?

Im pretty sure you have to create a attachment since you cant rotate a attachment. Then when u get a mouse input. You do a Dot Product and check if it’s behind a player. If it is then dont run a function. But if the mouse is infront. Then run a function

i have the same problem xd

maybe this will work? haven’t tested it yet

local hrp = character:WaitForChild("HumanoidRootPart")
local cam = workspace.Camera

MouseMoveFunction = Mouse.move:Connect(function()
	hrp.CFrame = CFrame.lookAt(hrp.Position,hrp.Position+cam.LookVector*Vector3.new(1,0,1)) * CFrame.Angles(0, Mouse.X/cam.ViewportSize.X - 1, 0)
end)

Would mouse.Hit be more preferable here? A CFrame value contains both positional and rotational data about a single point in the worldspace whereas mouse.Hit is a Vector3 value which just stores positional data.

It says lookVector is not a valid member of camera

Aren’t they both the same thing?

With a little bit of planning I was able to do this.

Let me share with you how.

First of all, we need a reference of what “looking forward” is…
The camera is always a good reference for this.

Then we simply need to get the angle from the dot product between the camera’s direction and the player’s direction. To get the directions, we can use CFrame.LookVector. Rather than using the current CFrame, we want to use the goal CFrame, and then only face the player to the goal if the angle is within a threshold, aka the player is facing forward.

That was my planning process. Then this is the script put together used in the video.

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse() 
local Char = Player.Character or Player.CharacterAdded:wait() 

local Camera = workspace.CurrentCamera

local AngleRange = math.rad(120) -- You can change this for more/less forward limiting 

MouseMoveFunction = Mouse.Move:Connect(function()
	local Direction = ((Mouse.Hit.p - Char.HumanoidRootPart.Position) * Vector3.new(1, 0, 1))
	local Goal = CFrame.new(Char.HumanoidRootPart.Position, Char.HumanoidRootPart.Position + Direction)

	local Angle = math.acos(math.clamp(Camera.CFrame.LookVector:Dot(Goal.LookVector), -1, 1))

	if Angle <=AngleRange then
		Char.HumanoidRootPart.CFrame = Goal
	end
end)

If there any problems with this let me know, but I tried to test it as much as I could for your situation. It may also help to disable Humanoid.AutoRotate if you need this while the character is moving.

Hope I helped, good luck!

Not necessarily, Mouse.hit.p is actually better in this case.

can you try this, i just fixed the problem you said

local hrp = character:WaitForChild("HumanoidRootPart")
local cam = workspace.Camera
local quarterPi = math.pi/4

MouseMoveFunction = Mouse.move:Connect(function()
	hrp.CFrame = CFrame.lookAt(hrp.Position,hrp.Position+cam.CFrame.LookVector*Vector3.new(1,0,1)) * CFrame.Angles(0, Mouse.X/cam.ViewportSize.X*quarterPi - quarterPi, 0)
end)

I tried it and it wouldn’t let me aim on the left side of character.