How would I make something happen when the player looks or faces at a part

What could I use to make something happen (like a sound plays are part changes color) when the player looks at a part? I’m not asking for a script just a datatype to create a script for it. Thanks :slight_smile:

If you have a collection of parts you are considering, you can check using the LookVector of the player against the position of the part from the player. You can use the dot product for this.

“Looks at a part” is kind of vague - does it have to be physically in front of the player, in the field of view, in the center of the screen, etc… These different cases require different approaches. For example, checking at the center of the screen wouldn’t be easy using the dot product method above, instead you would probably need the center coordinate of the viewport and cast a ray in the direction of the camera’s LookVector.

There’s no built-in event for looking at parts. You have to loop over the parts you are interested in yourself, or create your own event for it.

1 Like

Raycasting is the solution. You would need to use the LookVector from the player’s CFrame to get the directional vector.

1 Like

Got it, thank you so much for the detailed description!

I’ll see what I can do with raycasting too. Thank you so much for helping!

1 Like