Locomotion System: bug found (I think)

While playing with the locomotion system, I think I found a bug. It doesn’t occurs in all cases and is related to MonoBehaviour.Update execution order of 2 scripts:

  • NormalCharacterMotor.cs → NormalCharacterMotor.Update() calls UpdateFacingDirection() and UpdateVelocity()
  • PlatformCharacterController.cs → PlatformCharacterController.Update set motor.desiredMovementDirection

Note: I use the Locomotion System from the 3rd Person Shooter demo, but in your case, motor.desiredMovementDirection could be set by another Update function like in WanderingAICharacterController.cs …etc.

My understanding is that desiredMovementDirection must be set before UpdateFacingDirection() is called. With the official script, the order of execution is undetermined (2 Update functions called by the Unity engine). For a character controlled by the player, the direction can be adjusted constantly, so a bad order of execution result in a constant jitter of the character.

I have solved the problem by the use of the ExecutionOrderBehaviour class. In my program the NormalCharacterMotor and PlatformCharacterController classes inherit the ExecutionOrderBehaviour class. The 2 Update() functions were replaced by UpdateCustom(). The priority 0 was set to PlatformCharacterController, and 1 to NormalCharacterMotor.

When inversing the priority the bug is clearly visible. A cube can be added in top of the character to show the bug, it is more visible on sharp edge than on rounded meshes.

A bad Update execution order can cause the CharacterMotor to constantly be one frame behind the directions set by the PlatformCharacterController. So you essentially get a one frame delay of input. However, this shouldn’t cause jittering - unless maybe if your framerate is highly unstable from frame to frame.

Not being able to control the execution order of scripts is a limitation in Unity currently, though it can be worked around like with the ExecutionOrderBehaviour class.

Thank you for your answer. I can’t reproduce the bug on the “stock” 3rd person shooter demo.
For my app I have changed the camera management. I suspect the bug to come from my smoothing camera function. the desiredMovementDirection of the character is computed from the camera direction…

Edit: Finally I found my bug, for an unknown reason I have attached my camera to the character in place of the root of the hierarchy. So the camera turn to follow the mouse, the character follow the camera, the camera is turned by the character and so on :wink:

I’m glad to hear you got the issue solved!

Rune