Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

QUICK QUESTION: How do you tell when something is inside of a player's view?

Asked by 5 years ago

I want to be able to tell if a certain object is in the view of a player's screen. Obviously I could use Mouse.Target, but that would only work if the player looks directly at the object. I have absolutely no background knowledge on how to do this. I'm not sure if this is complicated as writing 4 different math equations, or if it's as easy as typing 3 lines, but it would help if someone pointed me in the right direction.

1
Something in the Camera could help, like a method User#19524 175 — 5y
0
Oh, here we go, I found something. steven73598233 75 — 5y
0
"WorldToScreenPoint: Takes a 3D world point and returns information regarding its 2D screen location, its depth, and whether it is visible on the screen or not.". Thanks for the suggestion. steven73598233 75 — 5y
0
check out my answer below v thebayou 441 — 5y

1 answer

Log in to vote
1
Answered by
thebayou 441 Moderation Voter
5 years ago
Edited 5 years ago

You can use Camera:WorldToScreenPoint() to see if a point is in front of your camera. Given that, we can then use Camera:GetPartsObscuringTarget() to see if any objects obscure the point.

local screenPos, inFront = camera:WorldToScreenPoint(part.Position)

-- Check screen bounds
if inFront and screenPos.X >= 0 and screenPos.Y >= 0 and screen.Pos.X <= mouse.ViewSizeX and screen.Pos.Y <= mouse.ViewSizeX then

    local obstacles = camera:GetPartsObscuringTarget({part.Position}, {})

    if obstacles == {} then
        print("Success!")
    else
        print("Object blocked")
    end
end

1
thebayou!! I SEE YOU PLAYING POLYGUNS WITH ME XD Zenith_Lord 93 — 5y
0
nice but you didn't have to comment to make me check your answer steven73598233 75 — 5y
0
lol sry just didn't want to be ninja'd by a comment thebayou 441 — 5y
Ad

Answer this question