#Raycasting optimisation

1 messages · Page 1 of 1 (latest)

vapid nest
#

Hello!

I'm implementing a system, where 'interactable' objects highlight if player is looking at them.
I am wondering about the most optimal approach.

First optimisation:
Since this requires for a ray to be cast fairly often to be responsive enough (every frame; or a relatively small time < 0.1s), I am trying to make the result of the raycast reusable

I fire this ray against all geometry and physical objects (let's call them solid from now on) at arbitrary max distance, cache the result and provide it to other scripts

Second optimisation:
I recognise the 'interactable' objects by a 'INTERACTABLE' layer, set within their collider in the Layer Overrides' "Include Layers..."; Generally, the 'interactable' objects can fall within the 'geometry' or 'physical' layer, so I consider it to be a sort of a modifier than a layer used directly on entities. The 'interactable' objects use a Monobehaviour script implementing an 'IInteractable' interface

Since I have the result of the broader raycast I mentioned before, I can check if the hit object's collider has the 'INTERACTABLE' layer present, and if it does, I use TryGetComponent to extract the reference to the script. I then call a 'Highlight' method on it.

So they question is: Is this a sensible approach?
Is it better to perform a broader Physics.Raycast on solid layers and then filter based on other criteria of the hit object (keep in mind that I may reuse the same raycast in other scripts, as getting what the player is looking at is generally useful for many reasons) OR should I instead perform multiple Physics.Raycast, but with more specific layers?

Also, is my current method of using a layer (the 'INTERACTABLE') as basically a tag, if I know all entities within this layer will have a 'IInteractable' script on them prior to getting the component, fine or should I just TryGetComponent every time (I am also checking if the collider hit by Raycast changed since last time, before doing anything again)?

#

Other than that, what is the correct way of highlighting the objects? I assume a shader that has a 'highlight' flag would suffice, unless there's some better way - like getting the orientation of maincamera and the distance to all objects using this material directly in the shader; but I think setting the effect from a script in this case could be better;

Later I might implement more than just a simple highlight, like an 'interact' overlay (similar to the original Deus Ex).

vital sparrow
# vapid nest Hello! I'm implementing a system, where 'interactable' objects highlight if pla...

an Interactable layer would allow you to filter out results, you shouldn't need to raycast against all solid objects then filter out later. Filtering out could even be just an overlap sphere check to get all the colliders in range that are on the interactable layer.
You could also just GetComponent rather than TryGetComponent if your design is that anything on the Interactable layer is required to have a component implementing IInteractable.

#

as for the actual "highlighting" of objects, that would depend on your shader. If the shader has a bool to toggle the highlight then thats how you would do it

vapid nest
#

Thank you.

I am wondering of the pros and cons of doing a single broad raycast if I'm going to reuse the results later by other scripts. In that case, I think it might be reasonable to do the filtering of the result on different scripts separately, if this Raycast has to happen (likely) every frame.

At least my reasoning is that it's better to cast once every frame for all solids, rather than to do 3-5 separate raycasts every frame for specific layers, but I don't have enough experience in Unity specifically to know whether that's not going against its design.

I am aware that what is better or worse for performance is best decided with a Profiler, but my main question is whether this is a fundamental design flaw in Unity or if it's acceptable for my situation.

split terrace
#

Why not cast one simple ray every frame to just get the first hit object? This way you wouldn't need to worry about optimization?

split terrace