Crawling side-view character in Unity

I implemented crawling for my human character controller in a cooperative platformer made with Unity, Mars 21.

Astronaut crawling

Astronaut crawling

Structure

The controller works with two MonoBehaviour scripts, AstronautPlayerControl.cs and AstronautMotor.cs, and an Animator component. The Animator Controller has 2 layers:

Motion layer for logic

The Motion Layer represents the character’s finite-state machine (FSM). A StateBehaviour containing the actual motion logic is added to each state, and all the transitions are immediate (transition time = 0). Below, you can see graph of the sub-state machine “Grounded”, with the grounded sub-states: Idle, Walk (hidden on the right), Turn 180 (for animated 180° turns), Crouched, Crawl and Idle-Crouch transition states.

Astronaut Animator Motion Layer Crawl

Motion Layer

The Crouched and Crawl state logic is pretty simple. Both StateBehaviours contain the following:

override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
	if (!motor.isGrounded) {
		motor.Fall();  // enter Fall state (apply gravity + fall animation)
		return;
	}
	if (!control.continueCrouchIntention) {  // is the player still holding D-pad / stick down?
		motor.StopCrouching();
		return;
	}
	motor.MoveCrawling(control.moveIntentionVector.x);
}

where control is an AstronautPlayerControl and motor is an AstronautMotor. MoveCrawling() moves the character by setting his velocity as it would with a normal walking 2D character, with 3 main differences:

  • the character preserves his current direction when moving backward (he does not turn)
  • the motion velocity is lower, and even lower when moving backward
  • the character starts crawling after any motion in the Crouched state (à la Metal Gear Solid), although this difference is merely visual

On the video, the character switches between Idle, IdleToCrouched, Crouched, Crawl and CrouchedToIdle. The transitions between IdleToCrouched and CrouchedToIdle allow the character to quickly interrupt crouching to stand up again smoothly and vice-versa.

Animation Layer for aesthetics

The Animation Layer animates the character model. An animation is attached to each state, and the transitions have short durations (0.1s).

Astronaut Animator Animation Layer Crawl

Animation Layer

Animations have been downloaded from mixamo after uploading the character model of the Astronaut.

Limitations

This approach has an important drawback: you have to duplicate the structure of the FSM between the two layers. Unity has a feature called Animation layer syncing which allows you to reuse an existing structure on multiple layers, but unfortunately it doesn’t support using different transition times on different layers (0 for the Motion layer and 0.1s for the Animation layer). For my next project, I will probably switch to an FSM completely in code (with my own StateMachine class), only using the Animator for actual animations.

Comments are closed