Limit user input until a player action is completed - Prince of Persia Classic 2D

I’m creating a 2D Classic Prince of Persia type Character Controller.

Mainly my focus now is on limiting user input until a player’s action is completed.

Basic example: the user holds shift and hits the left arrow. The prince takes one slow step to the left (even if you hold the left arrow it only registers that one move until it’s completed).

I’ve worked in several 2D Character Controllers from the documentation (move speed from Horizontal Axis input and applying velocity). They all work great, but not for this type of application.

Any advice I can research and dig deeper into would be appreciated.

Thanks!

Here is one simple example. It uses a coroutine. Put it on a game object and use the arrow keys to move left and right slowly.

#pragma strict

public var stepTime = 2.0;
public var stepDist = 0.75;
private var moving = false;

function Update() {

	if (!moving) {
		if (Input.GetKeyDown(KeyCode.RightArrow)) {
			SlowStep(transform.position + Vector3.right * stepDist, stepTime);
		}
		else if (Input.GetKeyDown(KeyCode.LeftArrow)) {
			SlowStep(transform.position + Vector3.left * stepDist, stepTime);
		}
	}
}

function SlowStep(newPos : Vector3, time : float) {
	var oldPos = transform.position;
	moving = true;
	var timer : float = 0.0;
	while (timer < time) {
		transform.position = Vector3.Lerp(oldPos, newPos, timer / time);
		timer += Time.deltaTime;
		yield;
	}
	transform.position = newPos;
	moving = false;
}

A coroutine is a nice way to solve this problem, but this may not be the coroutine for your game. You did not give much in the way of game mechanices…things like rotation or whether the character will interact with physical objects while moving. But this should give you a start.