Unity 2017.3 AIPath With Rigidbody Problem & Fix

Latest Unity Release 2017.3.0f3
A* Pathfinding Pro 4.1.8 Beta

Reproduction Steps

  1. Open Scene ‘Example10_LayeredGridGraph’
  2. Remove ‘Character Controller’ from ‘Bot’
  3. Add ‘Rigidbody’ & ‘Sphere Collider’ to ‘Bot’
  4. Run the scene

Expected Result
The bot should follow the cursor, using the rigidbody to move around

Actual Result
The bot appears to be stuck, only occasionally moving.

Fix - AIBase.cs

void FinalizeRotation (Quaternion nextRotation) {
	simulatedRotation = nextRotation;
    if( updateRotation ) {
        if( rigid != null ) rigid.MoveRotation(nextRotation); // <- NEW
        else tr.rotation = nextRotation;

FinalizePosition already uses the rigidbody if available, so that works.

I think this is because of how Unity has reworked Physics Transform syncing. Some info in this thread:


Thank you for reporting this, however I am unable to replicate it.

Ah. Managed to replicate it. Apparently my project had the compatibility option ‘Auto Sync Transforms’ enabled.
If I set it to false I get the result that you describe.

So if I understand this correctly, what happens is that my scripts set the position using rigidbody.MovePosition and the rotation using transform.rotation, however the next physics update the engine notices that the transform has changed (by setting the rotation) so it copies over the complete transform (rotation AND position) to the rigidbody resulting in that the rigidbody.MovePosition call is overwritten by old position info. Do you think this is what happens?
I’m not quite sure why it moves sometimes though… Possibly that happens when the rotation didn’t change at all?

Good point, I forgot the step where I turned off ‘Auto Sync Transforms’ :blush:

There is supposed to be a performance boost with it turned off, and I didn’t think I needed it, however, it turns out I needed to turn it back on…

I think your theory for why it happens is correct. And the case I ran into was where some of my players use CharacterController components. There is no way to set the physics position of a CharacterController (only Move & SimpleMove which aren’t the same as a teleport). So I had to set the transform position (network syncing), which ended up causing the same horrible not-really-moving sometimes behavior!

Oh well…