Locomotion

This page is part of the 3D Game Kit example

The character has several animations that can be blended to allow movement at any speed as well as to turn quickly when needed.

Mecanim

The Mecanim character's LocomotionSM state is a Sub State Machine containing a main Locomotion Blend Tree to allow for movement at any speed, left and right quick turn animations, and the Idle state mentioned on the Idle page:

Animancer

With Animancer, all the state logic is defined in the LocomotionState script which handles everything that applies while the character is moving along the ground except for things that also apply in other states as well (such as the Creature.OnAnimatorMove method described on the Movement page). It uses a Mixer State in place of the Blend Tree used by Mecanim:

using Animancer;
using System;
using UnityEngine;

public sealed class LocomotionState : CreatureState
{
    [SerializeField] private LinearMixerState.Transition _LocomotionMixer;
    [SerializeField] private ClipState.Transition _QuickTurnLeft;
    [SerializeField] private ClipState.Transition _QuickTurnRight;
    [SerializeField] private float _QuickTurnMoveSpeed = 2;
    [SerializeField] private float _QuickTurnAngle = 145;

    private AnimatedFloat _FootFall;

    private void Awake()
    {
        _QuickTurnLeft.Events.OnEnd =
            _QuickTurnRight.Events.OnEnd =
            () => Creature.Animancer.Play(_LocomotionMixer);

        _FootFall = new AnimatedFloat(Creature.Animancer, "FootFall");
    }

    public override bool CanEnterState => Creature.IsGrounded;

    private void OnEnable()
    {
        Creature.Animancer.Play(_LocomotionMixer);
    }

    private void FixedUpdate()
    {
        if (Creature.CheckMotionState())
            return;

        Creature.UpdateSpeedControl();
        _LocomotionMixer.State.Parameter = Creature.ForwardSpeed;

        UpdateRotation();
        UpdateAudio();
    }

    private void UpdateRotation()
    {
        if (!_LocomotionMixer.State.IsActive)
            return;

        if (!Creature.GetTurnAngles(Creature.Brain.Movement, out var currentAngle, out var targetAngle))
            return;

        if (Creature.ForwardSpeed > _QuickTurnMoveSpeed)
        {
            var deltaAngle = Mathf.DeltaAngle(currentAngle, targetAngle);
            if (Mathf.Abs(deltaAngle) > _QuickTurnAngle)
            {
                var turn = deltaAngle < 0 ? _QuickTurnLeft : _QuickTurnRight;

                if (turn.State == null || turn.State.Weight == 0)
                {
                    Creature.Animancer.Play(turn);
                    return;
                }
            }
        }

        Creature.TurnTowards(currentAngle, targetAngle, Creature.CurrentTurnSpeed);
    }

    [SerializeField] private UnityEvent _PlayFootstepAudio;
    private bool _CanPlayAudio;
    private bool _IsPlayingAudio;

    private void UpdateAudio()
    {
        const float Threshold = 0.01f;

        var footFallCurve = _FootFall.Value;
        if (footFallCurve > Threshold && !_IsPlayingAudio && _CanPlayAudio)
        {
            _IsPlayingAudio = true;
            _CanPlayAudio = false;

            _PlayFootstepAudio.Invoke();
        }
        else if (_IsPlayingAudio)
        {
            _IsPlayingAudio = false;
        }
        else if (footFallCurve < Threshold && !_CanPlayAudio)
        {
            _CanPlayAudio = true;
        }
    }
}

Movement Moving the character's position.
Turning Turning the character's rotation.