0
\$\begingroup\$

I've been searching through the Unity docs (https://docs.unity3d.com/ScriptReference/Physics.Raycast.html), but no matter where I look, I couldn't find exact behaviour of Physics.Raycast() method.

I've seen a lot of youtube videos using Physics.Raycast inside Update loop , so I started questioning behaviour of Physics.Raycast() method. (I discovered this vague unwritten rule: "If you use raycasts to ping objects it's okay to put it inside Update loop", same thing with Rigidbody.AddForce(x,y,z, ForceMode.Impulse) - apparently it's fine to use it inside Update)

So my question is: if I call Physics.Raycast() inside Update() does it execute immediately or does it get queued for execution on the next FixedUpdate frame? (documentation is missing explanation for this)

If it's the latter, how can I know which Physics methods get queued for the next FixedUpdate , and which happen instantly?

\$\endgroup\$
3
  • 2
    \$\begingroup\$ Doesn't the fact that the raycast returns its result immediately answer this question for you? You'll note that you don't need to write code for two separate frames, one to fire the ray and one to fetch the result a frame later. \$\endgroup\$
    – DMGregory
    Commented Sep 19, 2022 at 11:19
  • \$\begingroup\$ Now that you pointed it out it does make sense ,but a lot of videos and tutorials online do say everything physics related happens in FixedUpdate and Unity docs aren't specific to what happens in FixedUpdate, what happens immediately. Raycast method is inside Physics class thats why I asked how do I know what happens immediately and what gets queued for next FixedUpdate loop. But I guess I can assume every void method of Physics class gets queued and others happen immediately. Thank you \$\endgroup\$
    – tomek320
    Commented Sep 19, 2022 at 12:03
  • \$\begingroup\$ No, the physics STEP happens after FixedUpdate. That is, integrating accelerations, velocities, etc. by one fixed time step to get new positions and orientations for each dynamic body and its colliders, checking and resolving collisions and constraints. Any method YOU call executes when you call it. But if what you care about is a side effect of a physics step — like when the velocity or torque you applied in a function call results in a new position and orientation of the collider — those side effects are the things you need to wait for until after the next physics step. \$\endgroup\$
    – DMGregory
    Commented Sep 19, 2022 at 12:31

1 Answer 1

2
\$\begingroup\$

The raycast is executed as soon as you call the function. In fact, you are normally going to want to use the result of the raycast inside of the same update, and that just wouldn't be possible if the execution was delayed.

Now, I'm not an expert on how the physics engine works under the hood, but my understanding is that you can interact with physics objects at any point, not just FixedUpdate. The thing is that the changes that you make (adding a force to a rigidbody, for example) are not going to affect anything until the pyhisics engine runs its own internal update (which happens right after FixedUpdate). So, if you add a force in Update, and during the next frame (assuming the FixedUpdate has not happenend yet) you check the velocity of the object, you will see that it has not changed at all. But once the physics engine updates, it will work the same way as if you had added the force in FixedUpdate.

TL;DR is there is nothing magic about FixedUpdate, it simply is a way to execute your code right before the internal physics loop update.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .