Loss of boxcast contact between frames

Hello,

I have coded (in 2D) a script that allows my character to hang to the ceiling. It works like this:

  • Doing a boxcast to check a match with a specific layer in fixedUpdate
  • If we have a match and press up, the character attaches itself to the ceiling, i do this by freezing gravity and attaching my character as a child of the ceiling
  • If i press down or if i lose contact with the layer, i detach my player

Now the issue is that the object i attach to can move (thus setting it as parent) pretty fast, and when this is the case i seem to often lose contact and my character falls. I added a timer of 300ms before i detach my character but it doesn’t do the trick.

It seems that my boxcast is not quick enough to catch the moving object. Does this make sense? Should i not be doing my test in the fixed update loop?

Thanks

More likely might be that you are moving the object in Update() and box-casting at it in FixedUpdate(), or vice-versa. You want to keep it all consistent, as those two run out of sync.

If you’re using physics, use FixedUpdate() only, otherwise Update() only.

Here is some timing diagram help:

https://docs.unity3d.com/Manual/ExecutionOrder.html

Hello,

Thanks, i’ll try it out later. But isn’t the boxcast called only in the physics loop?

You can call it from wherever, but it’ll check the last positions the objects had in a physics update.

Try using the player’s rigidbody2D’s position instead of the transform.position as the origin of the box cast. That should ensure that the origin is correct with regards to the physics simulation.

I reopened my project and have not had the problem so far, weird.