Optimization for detecting if a player is viewing a part

I made a system for detecting if a player is looking at a certain part, I would like to optimize it or make it more efficient.
Any help / advice is appreciated!

Logic:

  • First I use touch detection - if a player is close enough to the part.
  • Then, while a player is close to the part - connect to WorldToScreenPoint function to see if a part can be visible on screen. This function is looping until the player is no longer close to the part.
  • If a part is visible on screen - connect to Raycast function to detect if the part is actually visible. This function is triggered by WorldToScreenPoint once.

I would also like to know when a player is done looking at the part, but I figured looping raycast would be expensive(?). Currently I use WorldToScreenPoint on the client to do that, once they actually had a view of the part (detected with raycast).

Any suggestions? Changes?
TIA

3 Likes

Will it be long range? If not I have a good simple solution with touch

you could do it another way where you check if the player is rotated towards the part using LookVector. Instead of touch function, you can do area detection but it’s not necessary. It’s probably better to use area detection tho since the touch function will be triggered several times but an area detection is different.

Yet again like I said those are not necessary but I think they would be cleaner and efficient. It’s up to you!!

It is long range, but I’d still like to listen.

I know, I made a IsBusy attribute and I check if the humanoid root part is still touching the touchbox from the client, to not overload the server.
I tried using dot product at first but it did not work well since you still have to loop this, and it is more difficult to check than if 2 parts are touching.

1 Like