Looks like a bug (when rigid2D has interpolation)

Hi! I’m very excited about your project and definitely going to use it in my game!

But it looks that I found a bug. After moving from Unity Navigation to A*Project I noticed that my agents move little bit slower. Also their speed is inconsistent - when they are crossing open area where nothing can stop them they often have micro drag or, vice versa, micro acceleration. So they go a little bit faster then a little bit slower, then again a little bit faster… These changes in speed are subtle, but still noticeable.

And it looks that I found a reason. I use Grid Graph and AIPath in 2D. Also my agents have Rigidbody2D and its Interpolate setting is set to “Interpolate”. There is an undocumented feature about this setting. If it’s set to “None”, then transform.position and rigidbody2D.position are exactly the same (as expected). But if it’s set to “Interpolate” or “Extrapolate”, then transform.position != rigidbody2D.position. This is because transform.position has some kind of interpolated value. And that is the reason why it can not be used as a source position for calculating target position. The rigid2D.position should be used instead. Or, in terms of code:

void FixedUpdate()
{
Vector2 offset = …
// rigid2D.MovePosition( transform.position + offset ); // Bug
rigid2D.MovePosition( rigid2D.position + offset ); // OK
}

But in the AIPath.MovementUpdateInternal( … ) method there is a line:

simulatedPosition = tr.position;

And all speed inconstancy dissappeared after I changed it to:

simulatedPosition = rigid2D.position;

Can it be treated as a bug?

I haven’t found any documentation about transform.position and rigidbody2D.position differences. But found these 2 answers:

https://answers.unity.com/questions/645353/what-is-the-diference-rigidbody2dtransformposition.html?childToView=1224452#answer-1224452

https://forum.unity.com/threads/best-way-to-get-rigidbody-position.526654/

1 Like

Just found a good article which explains rigidbody’s Interpolate property: https://blog.terresquall.com/2020/08/unity-rigidbodys-interpolate-property/

Hope this will be a good illustration why transform.position should not be used in FixedUpdate() when Interploate != None

1 Like

Kind reminder. This still feels like a bug. Please consider to fix it.

1 Like

After turning interpolation on of the AIPath agent, I had to change this line to fix it’s broken movement. @aron_granberg you probably forgot about this. Also there’s the line just below:

if (updateRotation) simulatedRotation = tr.rotation;

Will that also cause problems (barely noticeable maybe) rotating a interpolated rb2d?

Hi there, and good question! I think in this case I’d probably recommend still making a new thread rather than necro’ing an old thread, and providing a link to this thread. (Namely so the OP doesn’t get email notifications on a four year old post).

Go ahead and make a new post about it and I’ll let Aron know about it when I see it :+1: