Detect if something is on the player's camera?

I’m helping my sister make this weird horror thing. It would be cool if I could play a sound whenever the monster appears on the player’s camera. Is there a way I can do that?

6 Likes

You could probably use the ChildAdded event on the camera if you don’t put anything but monsters into the camera, but it would be better and more flexible to keep the code that handles the audio in the same place as the code that creates the monster.

Can’t really go into much more detail without more information.

The monster is just a model. I want to play a sound when the model enters the camera.

I found a way to do this, basically I checked if worldPoint is true and then used magnitude to make sure my player was close enough to the monster to actually see it:

http://wiki.roblox.com/index.php?title=API:Class/Camera/WorldToScreenPoint

Is there a better way I could do this?

10 Likes

The way you described is how I would do it!

1 Like

Alright then :slight_smile:

I think you’ll find that this is not a solution to your problem. worldPoint being true will be a false-positive detection anytime the monster’s location point is in the view frustum of the camera, no matter how many things might be between the camera and monster obstructing actual view of the monster. In other words, I’d expect the sound to trigger anytime you’re near enough (per your additional check) and the camera is pointed in the general direction of the monster, whether you can see it or not.

There is also a possible false-negative case. If you have a large monster, and represent it with only one point in space (say, the center), then half the monster can be on screen before your check detects that you can see it.

The way I would do this is whitelist the parts of the monster, and raycast from the camera position to select points on the monster–points that reasonably cover the extents of it–and see if you get a hit. Your sight distance check can just be the length of the ray.

8 Likes

As of right now it seems to be working fine, but that could be just because of how the map is set up. I was originally going to use raycasting, but then wouldn’t that only work if the camera was looking directly at the monster?

Like, assume the monster was on the right side of the screen. You can clearly see it, but you aren’t looking directly at it. Would raycasting catch that it’s on the screen then?

1 Like