#How to check if player looks at an object

42 messages · Page 1 of 1 (latest)

digital merlin
#

As title says, how to do it? I tried doing everything raycast, worldtoview point etc but nothing worked.
I dont want full script, I just want someone to fully explain me it

little mauve
#

In a local script you can use the player:getmouse() and you can use it to get the pos of the mouse In workspace I think

digital merlin
little mauve
#

Is the game first person?

digital merlin
little mauve
#

ok so you want what the camera is looking at?

digital merlin
#

what player is looking at

#

like is he faced forward the object

little mauve
#

isnt that what i said?

digital merlin
#

well, by camera i thought the whole camera

#

like you know that shows everything around you

#

because camera can be faced to an object but player not

#

and i dont want that

little mauve
#

ok

#

@digital merlin I made a script and it work but I didn't label what it does yet + it a short script

digital merlin
little mauve
#

im labeling it

digital merlin
#

al

little mauve
#

put in StarterCharacterScripts and in a LocalScript

local Player = game:GetService("Players")
local PlayerChar = script.Parent
local Camera = workspace.CurrentCamera

local CamPartHelp = Instance.new("Part")--[[I madee apart bc i cant get the Position of camera]]
CamPartHelp.Size = Vector3.new(0.1,0.1,0.1)--[[I size it bc why not]]

game:GetService("RunService").Stepped:Connect(function()
    
    CamPartHelp.CFrame = Camera.CFrame
    local Org = CamPartHelp.Position
    local Dir = (PlayerChar:FindFirstChild("Head").Position - Org).Unit * 50 --<higher number make the range more far
    local ray = Ray.new(Org,Dir)
    
    local hit--[[what it hit]], pos--[[Position of the place it hit]] = workspace:findPartOnRay(ray--[[The raycast]],PlayerChar--[[Ignore the player character]])
    
    if hit then
        print("I see ".. hit.Name)
    end
    
end)
little mauve
digital merlin
little mauve
#

i only make that part bc i dont think i can get position for the camera like i can only get the camera Cframe so i make the part get the camera cframe then turn the cframe into a position

twin oliveBOT
#

studio** You are now Level 10! **studio

little mauve
#

🥳

digital merlin
#

oh alr

digital merlin
little mauve
#

idk I'm not too great at bug fixing as all my script usually work perfectly

digital merlin
#

alr

little mauve
#

ima check if ik a way to fix it

little mauve
little mauve
#

Weird it didn't happen to me when I was testing

digital merlin
#

weird

gentle galleon
#

also delete the camparthelp thing you do "Camera.CFrame.Position" to get the camera position

#

@digital merlin

digital merlin
#

🤑

gentle galleon
#

coems 🤑

digital merlin
#

coems 🤑

cosmic whale
#

you can use raycast in order to help for this

#

oh sorry