RootMotion + NavMeshAgent

The goal here is to propose an implementation of the Root Motion system for controllers using a NavMeshAgent.

Please note that this feature is experimental and may require some adjustments on the implementation to work.

In this exemple the player controller is organized as follow:

  • PlayerObject (contains the NavMeshAgent and the script VisualAvatar)

    • AvatarObject ("root" -> contains the Animator)

      • Mesh

      • Armature

        • Hips

When used, the root motion will transfer any movement of the Hips to the AvatarObject (the root).

The script VisualAvatar is responsible for updating the position of the PlayerObject from the position of the AvatarObject (see below)

VisualAvatar
[SerializeField] private Transform m_visual;
[SerializeField] private NavMeshAgent agent;
...

private void LateUpdate()
{
	if (!m_visual) return;

	if (photonView.AmOwner) {
		agent.nextPosition = transform.TransformPoint(m_visual.localPosition);
	}
	
	m_visual.localPosition = new Vector3(0, m_visual.localPosition.y, 0);
}

Please note 3 things here:

  • m_visual is the transform of the AvatarObject

  • The position update must be in LateUpdate, as the Root Motion system operates in the Update state of Unity

  • In a networked environment, we want to set the position of the PlayerObject only if we are the owner of it

Last updated