Ad
Log in to vote
0

Help checking if enemy is on player's screen?

Asked by 18 days ago

I'm trying to make a script that plays a sound when the player looks at a monster. After some research, I have this script here:

local Camera = workspace.Camera
local part = workspace.Enemies.Chaser.UpperTorso
local _, withinScreenBounds = Camera:WorldToScreenPoint(part.Position)
while wait(0.1) do
if withinScreenBounds then
    print("looked")
    end
    end

The problem with this is that it only ever works after the player respawns. With the while loop it does the same thing, when the player respawns it will start looping. I've put this into a localscript located inside StarterGui. I've seen some people talking about raycasting but I'm not quite sure how to set that up. Is there any way to make the script above constantly check? Or an alternative? Any help would be appreciated.

0
use workspace.CurrentCamera T3_MasterGamer 1736 — 17d

1 answer

Log in to vote
5
Answered by 18 days ago

Your issue is you only check if the monster is on the screen once.

All you have to do is shuffle your code around so it is constantly checking whether the monster is on the screen

local Camera = workspace.Camera
local part = workspace.Enemies.Chaser.UpperTorso

while wait(0.1) do
    local _, withinScreenBounds = Camera:WorldToScreenPoint(part.Position)
    if withinScreenBounds then
        print("looked")
    end
end
1
use workspace.CurrentCamera T3_MasterGamer 1736 — 17d
Ad

Answer this question