Character rotation with mouse not working properly

I’m creating a simple script where your character will face wherever your mouse is pointing. I got it half working but putting your mouse behind the character doesn’t work. I’m not good with math and I don’t know how to fix this. Any help is much appreciated.

This is what it looks like now:
https://gyazo.com/cf715a661e55b218f0ad2a5ca9190a18

Code:

local player = game.Players.LocalPlayer;
local mouse = player:GetMouse();
local character = player.Character or player.CharacterAdded:Wait();
local root = character:WaitForChild("HumanoidRootPart");

game:GetService("RunService").RenderStepped:Connect(function(step)
	local rx, ry, rz = mouse.Hit:ToOrientation();
	root.CFrame = CFrame.new(root.CFrame.p) * CFrame.fromOrientation(0, ry, 0);
end)

Try using Mouse.TargetFilter or raycasts. Not sure about you, but raycasts might be a better option here.

I’ve done something like this and I can hand you a example if you want once I’m home.

1 Like

TargetFilter didn’t help at all. Might be best to use raycasts.

local Tweens = game:GetService("TweenService")
local Run = game:GetService("RunService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local HRP = Character:WaitForChild("HumanoidRootPart")
local Mouse = Player:GetMouse()

Run.RenderStepped:Connect(function()
	local Tween = Tweens:Create(HRP, TweenInfo.new(0.2), {CFrame = CFrame.lookAt(HRP.CFrame.Position, Vector3.new(Mouse.Hit.Position.X, HRP.CFrame.Position.Y, Mouse.Hit.Position.Z))})
	Tween:Play()
end)
1 Like

This did the trick, thanks a bunch.