How to make HumanoidRootPart not look behind player camera

As the title says i would like to make it not go behind like in @Headstackk’s game called Weaponry. I am not that experienced in CFrame so i am sorry for asking a script

what i have right now

what i want

1 Like

Could you send your CFrame code?

1 Like

I do not know how to do the script format but here is an image instead

1 Like

1 Like

Thank you, so it is like in discord

1 Like

Try this out:

local PS = game:GetService("Players")
local RNS = game:GetService("RunService")

local player = PS.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local HRP = char:WaitForChild("HumanoidRootPart")
local mouse = player:GetMouse()
local lerpUpdate = .5
local minRotate, maxRotate = -60, 60

RNS.RenderStepped:Connect(function()
	local hit = mouse.Hit.Position		
	local aimPos = Vector3.new(hit.X, HRP.Position.Y, hit.Z)
	local goal = CFrame.lookAt(HRP.Position, aimPos) do
		local x,y,z = goal:ToOrientation()
		y = math.rad(math.clamp(math.deg(y), minRotate, maxRotate))
		
		goal = CFrame.fromOrientation(x,y,z) + HRP.Position
	end
	
	hum.AutoRotate = false
	HRP.CFrame = HRP.CFrame:Lerp(goal, lerpUpdate/2)
end)
1 Like

It does work but

local Game = game
local Workspace = workspace
local RunService = Game:GetService("RunService")
local Players = Game:GetService("Players")
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Camera = Workspace.CurrentCamera

local function OnRenderStep()
	local _, CameraY, _ = Camera.CFrame:ToOrientation()
	HumanoidRootPart.CFrame = CFrame.lookAt(HumanoidRootPart.Position, Vector3.new(Mouse.Hit.X, HumanoidRootPart.CFrame.Y, Mouse.Hit.Z))
	local _, HumanoidRootPartY, _ = HumanoidRootPart.CFrame:ToOrientation()
	HumanoidRootPart.CFrame = CFrame.new(HumanoidRootPart.Position) * CFrame.fromOrientation(0, math.clamp(HumanoidRootPartY, math.min(CameraY - math.pi / 2.5, CameraY + math.pi / 2.5), math.max(CameraY - math.pi / 2.5, CameraY + math.pi / 2.5)), 0)
end

RunService.RenderStepped:Connect(OnRenderStep)
2 Likes

It snaps alot, thanks for your help tho

When you move the mouse behind the character the character’s Y-axis rotation will be clamped to either side, the character will only continue to rotate once the mouse is moved in front of it, hence the snapping is unavoidable.

Oh i see but is there a way to make it smooth? I tried using lerp and it didn’t work well.

local Game = game
local Workspace = workspace
local RunService = Game:GetService("RunService")
local Players = Game:GetService("Players")
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local Character = Player.Character or Player.CharacterAdded:Wait()
local Camera = Workspace.CurrentCamera

while true do
	local Time = 0
	while true do
		if Time > math.deg(math.pi / 2) then break end
		local Pivot = Character:GetPivot()
		local CF = CFrame.lookAt(Pivot.Position, Vector3.new(Mouse.Hit.X, Pivot.Y, Mouse.Hit.Z))
		Character:PivotTo(Pivot:Lerp(CF, Time / math.deg(math.pi / 2)))
		Pivot = Character:GetPivot()
		local _, CameraY, _ = Camera.CFrame:ToOrientation()
		local _, CharacterY, _ = Pivot:ToOrientation()
		Character:PivotTo(CFrame.new(Pivot.Position) * CFrame.fromOrientation(0, math.clamp(CharacterY, math.min(CameraY - math.pi / 2.5, CameraY + math.pi / 2.5), math.max(CameraY - math.pi / 2.5, CameraY + math.pi / 2.5)), 0))
		Time += RunService.RenderStepped:Wait()
	end
end
1 Like