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.
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.
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
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)