How to make a character run by alternately hitting two buttons?

As for old games such as Konami’s Track and Field, where the feedback you get is that “the faster you hit, the faster you run”, I’d like to know if it’s possible to get such mechanic to work in Unity and how.

I don’t know if it’s possible to “override” the InputManager somehow or if it’s possible to create the mechanic within the default CharacterController (right now I’m testing it on the 3D 3rd Person one, but I’m looking forward to develop a 2D side-scrolling kind of game)


I’m a true beginner to both JS/C# scripting and Unity, so I apologize if I appear lazy or unappropriate, I don’t mean it at all! :slight_smile:

I’m asking because I really can’t easily figure where to begin.

Actually, I do have a little scripting background (mainly because of Adventure Game Studio) and a little knowledge of UDK environment, so I MAY (hopefully) understand a little more than expected by my premise.

I’m trying to get acquainted to Unity as much as I can.

I’ll appreciate any pointer you may give me :smiley:

Thanks a lot!

Use a script to check for alternating states of a key. This can be done by simply checking for an actual press-in of the key rather than whether the key is being pressed.

This comes down to the difference between Input.GetKey and Input.GetKeyDown. The first returns true every frame/time whereas the latter returns true only if there is a change in state (from up to down), ruling out a scenario where the key is being pressed uninterruptedly.

if (Input.GetKeyDown(KeyCode.W)) //or any other key you want to use as movement trigger
   {
      transform.position += transform.forward * speed * Time.deltaTime;
   }

As to editting the character controller to do this, it’s possible but you would have to apply the same basic idea to the conditionals it uses to check for input.

EDIT: Woops, forgot the 2-key part. One way of doing this would be:

if (Input.GetKeyDown("key 1") && keyAlternate == false)
  {
    transform.position += transform.forward * speed * Time.deltaTime;
    keyAlternate = true;
  }
else if (Input.GetKeyDown("key 2") && keyAlternate == true)
  {
    transform.position += transform.forward * speed * Time.deltaTime;
    keyAlternate = false;
  }

Or the somewhat more sofisticated way that is described below.

For the alternating, it’s just basic programming. Make a var to remember which key you are on, the check that var during the press:

char moveLetter="a"; // alternate a,s,a,s...

// update:
if(Input.GetKeyDown(moveLetter)) {
  // got that key, switch the key we need for next time:
  if(moveKey=="a") moveKey="s"; else moveKey="a";

  // get a burst of speed

The other part is, the standard controller sets your speed directly from the arrow or WS keys. You’d have to change that so it doesn’t recalc speed each frame. Instead it adds to speed for each key press, and remembers when the last key was pressed. If it’s been more than 1/2-sec, slow down.

Thanks to you both! I’m finally gettin’ something :smiley:

At the moment, even if I think I understood what Owen Reynolds wrote, it felt easier, to me, to follow the “less sophisticated” one, by DeBunked since I actually tried something similar but “lost myself” through if statements.

Anyway, I’m now getting him to run horizontally on the x axis, but I suppose that I have to code something for the colliders, since it works nice on a Z = 0 rotated surface, but if I try to get him run over a Z = 30 one, it clips through it.

I’ve also noticed that changing

transform.position += transform.forward * speed * Time.deltaTime;

to

transform.position.x += transform.position.x * speed * Time.deltaTime;

seems to boost the character speed each time the key is pressed, but I believe it only multiplies his position on the x axis, so he’s not “running” as we mean it.

Each time I encounter a Time.deltaTime it always has some sort of “wow” effect, because I still have to really understand what it actually means (even if I read the documentation). In this very case, does it mean that it’s multiplying it’s position.x by the value of the speed variable by the time that past between one click and another (if a frame can be intended in such way, in this case)?

Thanks a lot, so far!