Raycasts will not detect Colliders for which the Raycast origin is inside the Collider. In all these examples FixedUpdate is used rather than Update. Please see Order of Execution for Event Functions to understand the difference between Update and FixedUpdate, and to see how they relate to physics queries.
Why then is Physics.RayCast done specifically in FixedUpdate and not Update in these examples, so much so that the docs make a point of it? AFAIK, and I might be mistaken, it is purely a query and doesn’t change any thing in the physics of the scene? Yet things broke for me when I tried using raycasting in Update instead.
So I was trying to use Physics.Raycast in Update, which was terribly sensitive to frame rate, but I couldn’t really find a reason why that should be. I was using ScreenPointToRay(Input.mousePosition) to check ray-collision with a static collider that was created on scene load and never moved, nor did the gameObject it was attached to have a RigidBody or any other interaction with the physics engine. Even when the mouse was unmoving, equivalent rays would at times hit and at times miss the collider when the frame rate was high. This issue never happened at lower frame rate or when I did the raycasting in FixedUpdate.
I searched around past posts for a bit but couldn’t find the explanation. A close candidate might be that in high framerate, FixedUpdate isn’t called between frames, and if raycast does something there (or uses some values computed there) it would fail.
But again, Raycast returns its value immediately, and any values computed in FixedUpdate shouldn’t have changed since neither the collider, camera nor mouse were moving, so I’m not sure I have any idea of why I observed this behavior. Posting this here in case someone can enlighten me.
You may use Raycast (or any other physics cast) in either Update or FixedUpdate. A Raycast is just a query against the current state of the physics scene.
As a general rule, the decision on where to use Raycast on, Update or FixedUpdate, depends on what you are doing with the result of the query:
If you’re using the raycast to drive other physics actions, such as applying forces, moving physics elements, visibility queries, taking decisions that depend on the physical environment etc, then you should always use FixedUpdate.
If the result of the raycast is used to do visual effects, audio effects etc, or user interactions like in your case, then Update is perfectly fine.
As said, a Raycast is a query against the current state of the physics scene. In a typical project, the physics state itself changes after FixedUpdate. Update and FixedUpdate are in practice two separate execution flows: several FixedUpdate calls (with their physics updates) may happen between each Update call, and -most likely- several Update calls may happen between each FixedUpdate call.
Using Raycast in FixedUpdate gives you a result for each and every physics update.
Using Raycast in Update gives you a result on each visual frame update, which corresponds to the latest physics state at that moment.
Update is basically a rendering (and input) system update. FixedUpdate is a physics (simulation) system update. Raycasts both test and belong to physics simulation side of the engine.
Practical reason:
Imagine setting up an AI agent that uses Physics.Raycast calls to check player visibility. If you place that raycast check in Update then what will happen is that this AI will have considerably faster reaction times on faster computers and lower reaction times on much older machines. The result of this will be that AI’s/game difficulty will depend on how fast given player’s computer is. FixedUpdate is called at the same frequency no matter how fast your machine is thus removing this difference.