Hello,
I am having some trouble putting an accurate ground check on my character object.
I have a capsule2D collider for the player, and a box2D collider for the feet of the player. For ground checking, I am using physics2d.BoxCast() to cast a box from the feet box2D collider.
The white box is the origin of the box cast. the red box is the cast distance (which turns blue if it collides with ground). and the green horizontal box at the bottom is the ground
As you can see, the white box should be perfectly overlapping with the box 2D collider at the bottom of the capsule, but it is lagging behind. It’s not just the debug rays of the box being drawn that are lagging, because the ground check should be hitting true at this frame, which it is not
The code for reference running on FixedUpdate, I have also used Update and LateUpdate with no luck. And use the rigidbody.position for boxcast origin
You don’t need to use any of this casting at all to detect contacts. This kind of stuff is pushed out in lots of tutorials and is a super difficult way to do it. Regardless, if you have a collider then you can simply use Collider2D.Cast.
Also note, Transform.position is NOT the same as Rigidbody2D.position if you’re interpolating. It is not the ground truth for position (no pun intended).
You should however ignore all those shape casts and simply query what contacts you have. You can use the IsTouching command to ask if you’re touching anything such as ground. You can even provide a ContactFilter2D which you can configure in the inspector and even set the direction of the contact you consider grounded etc.
Doing the raycast in FixedUpdate is the correct way if you use this for a physics (rigidbody) object.
Since the boxes seem to be in completely opposite directions, perhaps you made a +/- mistake somewhere? If the brown (that’s not white! but hey, maybe it’s the image compression) box were mirrored along X it would almost overlap the green box, and that small amount of difference could indicate a one-frame-off issue. Is the rigidbody perhaps interpolated? If so, disable that while testing.
Collider2D.Cast seems to give out the same problem as physics2D.BoxCast, where it’s lagging behind a few frames.
Thanks for the reference on Rigidbody2D.istouching(). I did not use it since this triggers for when any collider on the player object is touching, and i only want to check for the feet box2D collider at the bottom of the player object. but i did see that there is a Collider2D.isTouching(), so i used that on the feet box2D Collider instead.
I mean physics is updated at different rate than the regular Update loop. You can likely be casting a collider for a few frames before its updated physics wise.
As above, it doesn’t lag, it detects colliders as they are. There’s simply no possible way things like this “lag”. You should re-read my post about Transform.position not being where colliders are if you’re using interpolation; refer to the authority, Rigidbody2D.position instead.
Also, if you’re setting the Transform to try to control physics (you should never do this) then you’ll find that this is causing you issues; you should use the Rigidbody2D API to cause movement.
Physics isn’t running per-frame unless you ask it to by changing the simulation mode. Update != FixedUpdate.