Doing a raycast outside of _physics_process()?

Godot Version

v4.2.stable.official [46dc27791]

Question

In a project, I am using a ShapeCast2D node by calling its ‘force_shapecast_update()’ method. A UI button, once pressed, calls a function that does the shapecast. The shapecast is done outside of _physics_process().

Are there any negatives to calling a physics function, such as 'force_shapecast_update(), outside of _physics_process() ?

Not much.

You are only calling an extra shapecast and ‘wasting’ a bit of performance.
But in your case you only rarely do so, it shouldn’t be any problem.


Also, although the Shapecast/Raycast nodes are very useful and easy to use, you can get performance issues if you have a lot of them active at the same time. (usually not a problem, but something to keep in mind)

The reason for that is that a shape/ray cast node is doing the calculations every physics frame.
So it might be a good idea to use them in code, to only test for collisions when you need them.

For example. For AI vision raycasts, I usually use a timer of 150ms for each raycast test, it is more than fast enough to act on the AI.
If I were to use Raycast nodes instead, it would be calculated each 16ms (60fps), about 10x slower.

But then again, raycasts barely use any time. So more likely you don’t need that optimization.

Thanks for the good info Gustjc.
I was most concerned with calling a physics function outside of the physics step.

I have my ShapeCast2D’s ‘enabled’ property set to false. The documentation says you can set it to false and still call force_shapecast_update(). I assume that the shapecast only fires when I call force_shapecast_update(), but I haven’t tested it.

1 Like