I’m having issues with getting raycasts from a player’s gun to reliably hit a moving target. Half the time the rays seem to pass through the target without registering (despite there being two potential colliders to hit).
Targets are being moved by Rigidbody.MovePosition() and the only part of the system not using fixed updates is the input detection for the gun. The rigidbody is located on the root gameobject of the target prefab (colliders G/Os are children).
Is there something I’m not taking into account when using the physics engine in this way?
It is possible that, depending on when you are performing the traces relative to when you are moving the rigidbodies, you are tracing against where the rigidbodies were instead of where you expect them to be.
I think that MovePosition() also might not immediately move the rigidbody to the specified position; I think is calculating what forces are required to move the rigidbody to the desired position and then applying those forces. Maybe. It might depend on whether the rigidbody is kinematic.
Add debug checks that log the position of the rigidbody (rigidbody.position) and the transform (transform.position) before and after your calls to MovePosition(). You may want to do the same for the collider if it is on a separate game object. You may also want to check the position of the collider before your raycast to see if it is where you expect it to be at that moment.
Also, the documentation for MovePosition/Rotation states that it is intended to be used for kinematic rigidbodies.