Player look towards mouse stuck on parts

The player currently looks towards the mouse, but sometimes stalls when the mouse goes over objects like walls and what not. I want the mouse to ignore all walls and everything and the player looks towards the mouse no matter what.

local player = game.Players.LocalPlayer
repeat wait() until player:GetMouse() and player.Character
local mouse = player:GetMouse()
local character = player.Character
local render = game:GetService("RunService")

local atan2, pi, dir, torso, torsoPos = math.atan2, math.pi

while true do
		render.RenderStepped:Wait()
		torso = character:FindFirstChild("HumanoidRootPart")
		if torso ~= nil then
			torsoPos = torso.Position
			dir = (mouse.Hit.p - torsoPos).unit
			torso.CFrame = CFrame.new(torsoPos) * CFrame.Angles(0, atan2(dir.X, dir.Z) + pi, 0)
		end
end

It may be worth mentioning this is an oldish script I had help making and for some reason the 7th line is underlined red, but everything still works.

PlayerMouse.Hit is a CFrame of where a ray from the PlayerMouse hit onto a Part.

You can infact get this ray normalised (from PlayerMouse.UnitRay) and therefore retrieve the Direction.

Perhaps try replacing mouse.Hit.p with mouse.UnitRay.Direction

Thanks for the reply. I tried your suggestion but it doesn’t seem to work. There are no errors, but the player just stops looking towards the mouse entirely.

I think he meant to replace this part:
dir = (mouse.Hit.p - torsoPos).unit
with this:
dir = mouse.UnitRay.Direction

Thanks that doesn’t seem to worth either though, but it does do something. The player looks towards the mouse but only when it’s on the upper half of the screen

Instead of using atan2 maybe try using the two-vector constructor:

torso.CFrame = CFrame.new(torsoPos, torsoPos + dir * Vector3.new(1, 0, 1))

Or if you’d rather not use the deprecated constructor, use CFrame.fromMatrix()

dir = (mouse.UnitRay.Direction * Vector3.new(1, 0, 1)).Unit -- project onto XZ
local right = dir:Cross(Vector3.new(0, 1, 0))
local up = right:Cross(dir)
torso.CFrame = CFrame.fromMatrix(torsoPos, right, up)
1 Like

Sorry i know it’s a late reply, but your top recommendation still gets the mouse stuck on parts, the other had the same problem as the one above where it only works a little bit when the mouse is on the top half of the screen, but also doesn’t seem to get stuck on parts. My game is in a fixed (mostly) top-down angle if that matters with this code (which i assumed it wouldn’t).

It’s alright. I didn’t know about the top-down component though, so that gave me an idea to use a raycast from the mouse’s position on the screen. Using a whitelist containing just the parts that make up the floor, you can get a position for a mouse hit that ignores walls and other obstacles.

-- uis = UserInputService
-- cam = CurrentCamera
-- hrp = HumanoidRootPart
pos = uis:GetMouseLocation()
ray = cam:ScreenPointToRay(pos.X, pos.Y - 36)
ray = Ray.new(ray.Origin, ray.Direction * 100)
part, pos = workspace:FindPartOnRayWithWhitelist(ray, {workspace.Baseplate})
if (part) then
	hrp.CFrame = CFrame.new(hrp.Position, Vector3.new(pos.X, hrp.Position.Y, pos.Z))
end

Here’s a test place I threw together in case you want to take apart the script I used:
MouseLook.rbxl (21.1 KB)

1 Like