teaching machines

CS 491 Lecture 21 – Walk Cycles

April 19, 2016 by . Filed under gamedev3, lectures, spring 2016.

TODO

Lab

Today we’re going to make our alien walk! Open up your Unity project and your alien’s Blender file stored within.

Arms

Our alien needs some arms to make walking easier. Extrude out some simple arms, and add some bones to the armature to pass through them.

Walk Cycle

We’re going to animate our alien walking. Our breakdown of the frames will follow illustrator Dermot O Conner’s walk cycle tutorials. In brief, we’ll break down the animation into four distinct poses. Those four poses will be mirrored to produce a 9-key sandwich:

Walk Cycle

Let’s observe what happens on each frame.

Contact
In this frame, the heel of the leading foot just touches the ground. The trailing foot is at its sharpest bend, propelling the body forward while getting left behind. The feet are the farther apart here than they are in any of the other poses. What does that say about the bend of the legs? The arms are posed opposite the legs. They have not yet reached their extremes. The torso is slightly twisted to follow the arm motion.
Recoil
In this frame, the leading foot is taking over control as the trailing foot lifts from the ground. The leading foot is flat on the ground. The trailing knee bends to clear the foot from the ground. The leading knee also bends a bit, meaning the whole body drops. The head is at its lowest point. The arms have swung to their extreme positions.
Passing
In this frame, the leading and trailing limbs are swapping places. The elbow bend is minimal. The planted foot is still flat on the ground but the planted leg is straightening to carry the lifting foot forward. The torso untwists.
High-point
In this frame, the planted foot begins to bend upward and the planted leg straightens to its extreme as the body lurches forward. This causes the upper body and head to rise. The leading knee is at its maximum bend.

Some of the tutorials you follow may advise you to also translate the object forward along its path. I’ve not gotten this to work reliably; my characters tend to veer off course a bit. Instead, I do an in-place walk cycle animation in Blender and I move the object forward with code in Unity.

Posing 4

Open the Dope Sheet and switch to the Action Editor buttons. Create a new animation clip named Walk.

Enter pose mode. Hit N over the 3D editor to open the right-panel. Switch from quaternions to XYZ euler angles. (I can’t seem to rotate beyond 360 degrees with the quaternions, and the meaning of their numbers is less obvious.)

Create the four frames described above based on their descriptions and what you see in the walk cycle image. Leave a few frames between each, but don’t be too concerned with the timing. I’ll assume you record at frames 1, 5, 9, and 13. You can move the keyframes later.

Remember the recipe for recording!

  1. Position bones by translating and rotating.
  2. Select all the bones.
  3. Record a keyframe with the I key. A keyframe of type LocRot should be sufficient. (I had been recommending Visual LocRot, which I thought fixed a problem I had been encountering. I was wrong.)

Mirroring

With the four distinct keyframes recorded, it’s time to generate the rest as mirrors. Scrub back to your first contact pose in the dope sheet. In the 3D editor, select Pose / Copy Pose. Scrub to frame 17. Select Pose / Paste X-Flipped Pose. Select all bones and record a LocRot keyframe.

Do the same with the remaining poses at appropriate frame numbers.

The final contact pose is identical to the first. There’s no need to x-flip it. Save your file and head to Unity!

Importing into Unity

Did your walk cycle import into Unity? Add the walk clip to the alien’s animation state machine. Add a Speed parameter and transition from Idle to Walk only when speed goes above 0. Transition out only when speed goes below some small value.

Playtest and manually set the speed parameter in the Animator window to test the animation. Good news?

Forward Motion

Add a collider and Rigidbody to the alien so that gravity will put it down and so that it can’t pass through walls.

Let’s make walking actually produce progressive motion. Keys A and D will turn the alien. Keys W and S will cause the alien to move forward or backward in the current direction.

Add an AlienController script to the alien. Get references to the Rigidbody and Animator components in Start. In Update, we need to send the Speed parameter to the animator. What is the alien’s speed? Well, rigidbody.velocity. But this might get adjusted by other forces, like gravity or projectiles. We want to consider the speed only in the forward direction to trigger walking. So, we use the dot product to filter out only the forward component of the velocity vector.

float h = Input.GetAxis("Horizontal");                                                                                                                        
float v = Input.GetAxis("Vertical"); 

rigidbody.rotation *= Quaternion.AngleAxis(h, Vector3.up);
rigidbody.velocity = transform.forward * v;

animator.SetFloat("Speed", Mathf.Abs(Vector3.Dot(transform.forward, rigidbody.velocity)));