- Home /
What do I need to know when caching the results of a raycast every frame?
In our game, we perform a raycast in 3D space based on the location of the cursor in screen space. We want to cache the results of this raycast for different uses so we're not performing the same raycast in multiple place. We want the results to be as accurate as possible from the player's perspective. We don't want to present information to the player based on the results of this ray trace that are fast or slow by one frame.
The meat of this question is:
In what object and function should we perform this raycast and save the results?
We're unfamiliar with the order that different objects are processed and the order that different udpate functions are called. Thank you for your help.
Answer by jonas-echterhoff · Aug 04, 2010 at 09:44 AM
What you could do is to have a CachedRaycast class, which stores the raycast hit, and the value of Time.time. Then in a getter function, check if Time.time has changed, if it has, perform a new raycast, otherwise just return the cached result.