The walk animations for the characters have been pretty wonky for long enough! It was finally time to address the issues – or try to.
The problem
The main issue has been the stopping. The characters have usually just walked in place like on some magical treadmill. Instead of walking into a graceful stop.
This happened because I had no stop state, just a transition from walk to idle. And during the transition the walk loop would keep playing. I would need to add a stop animation to address the stopping issue, but the problem is that you need to play a different animation based on the state of the animation: which foot is planted.
This bugged me, but I did not know how to fix it.
But today when I showed a friend how to add events to animations, I figured out how to add the cadence and It was actually very, very easy.
The solution
We need to do 3 things in order to make this happen:
- Animations for stopping from both cadences. Left foot up and right feet up.
- A code that tells the animator what cadence the character is in.
- The events on the walk animations for triggering custom functions in the cadence code.
For the animations I have purchased an animation set from the Unity Asset Store -called Movement Animset Pro. It is a pretty extensive set with all of the locomotion animations I will ever need for this project.
To set up the events, all you need to do is load up the animation file, and for each clip, go trough the timeline and add the cadence events.
I named my events L and R to keep it simple. L for when the left foot is planted and R for when the right foot is planted. For each animation I also added an event for the first keyframe to make sure the animations begin with the correct cadence. Just in case. I added these events for all animations that are used in the locomotion set-up.
I added the events not for the foot planting on the floor, but slightly before As there is some transition to the stop walk event.
My locomotion set-up is still pretty simple. It consists of 3 parts: beginning to move, moving and stopping. The values the are fed into the animator are walk speed and turn rate.
The animator sets the walk animation in motion with start walk animation when the speed is more than 0.2. Once this animation has played trough, we move on over to the walk/run blend tree state. This state is then happily ticking away, setting the cadence of the animator on or off based on which foot is planted.
As the speed goes below a certain threshold. We enter the stop animation stage, where we chose one of the 2 available options based on the cadence.
To send the event data to the animator, we need to add a Bool to the animator called “Cadence”. Then we need to create a C# script for the character that reads the animation events and sends them over to the animator.
The source code for this script is pasted here:
Cadence.cs source code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.AI;
public class Cadence : MonoBehaviour
{
private Animator CharController;
// Start is called before the first frame update
void Start()
{
CharController = this.GetComponent<Animator>();
}
public void L()
{
Debug.Log("Left foot down");
CharController.SetBool("Cadence", false);
}
public void R()
{
Debug.Log("right foot down");
CharController.SetBool("Cadence", true);
}
}
The script automatically finds the animator from the character and sends the events over. The screenshot is from the older version that still required you to set the animator by hand.
The new system is not perfect. But it is a lot better than it used to be. For sure I will revisit this later. But this will do for a while now!
Leave a Reply