Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Double Tap a keyboard button

Discussion in 'Scripting' started by moynzy, Feb 19, 2015.

  1. moynzy

    moynzy

    Joined:
    Oct 22, 2014
    Posts:
    82
    I'm using Unity C# to implement a prototype game.

    I have the basic movements, such as walk, run, and jump in all directions.

    However, I'm finding it hard to implement a function where the player must double tap in order to dodge/dash.

    Code (CSharp):
    1.             if(Input.GetButton("Vertical")){
    2.                 if(Input.GetButton("Run")){
    3.                     Run();
    4.                     moveDirection*=runMultiplier;
    5.                 }
    6.                 else{
    7.                     Walk();
    8.                 }
    9.             }
    10.             else if(Input.GetButton("Horizontal")){
    11.                     Strafe(); //move sideways
    12.                    
    13.             }
    14.             else{
    15.                 Idle();
    16.             }
    Below the Strafe function, I want to check if the player double taps the button. If they do, then change their moveDirection to something higher.

    I've tried searching online, but I only have found implementation of buttons being held. For example, WW to run, like in shooters.

    Would appreciate any positive directions to this approach. Thank you.
     
  2. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Use a timer to keep track of time between key presses. If it meets a certain criteria do stuff.
     
    ADNCG likes this.
  3. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    490
    x-_-pipe-_-d and moynzy like this.
  4. moynzy

    moynzy

    Joined:
    Oct 22, 2014
    Posts:
    82