Unreal Engine 4: Let’s Make a Double Tap Event

Let’s Make: A basic double tap event inside blueprint. It will determine if the user input is a single or double tap and if the button is held. This event can be used for anything but in our instance we will make it control the player speed.

Overview

An event which determines if the user has single or double tapped and if the input is still held down. At the end of our event we will apply a speed change to our character movement variable.

  1. Tapping once and holding will increase the player speed to a jog
  2. Tapping twice and holding will increase the player speed to a sprint
  3. Releasing the button will cause the character to walk

The current version will use:

  • Third Person Template
  • Blueprint Only
  • Uses Unreal Engine 4.8.3

Getting Started

An Unreal user on the forums was asking me about how to achieve a double tap check so I decided to make a tutorial about it.

We will create three types of double tap interaction. This is to show off a few way to achieve the desired event check.

Simple Double Tap

This version is simple but will cause the single tap event to fire off as well as the double tap.

This is useful if the single tap event has no purpose or code. But it causes problems if both events to do something different.

Timer Double Tap

The second event will use timers to distinguish either a single or double tap. The event waits until the timer has counted down before calling the desired effect. This will avoid the previous problem of the single or released event from being called early.

The downside is the need of timers and extra events attached to each. Yet, this isn’t a bad way to create the desired double tap effect and keeps code clean.

Delay Double Tap

The third event will use retriggerable delays instead of the timer and custom events. The main difference is avoiding the custom events which makes this code easier to reuse.

It depends on personal preference and project needs for which is better.

Creating the project

Start by creating a new project or opening an existing one. This tutorial will use the third person template for demonstration purposes.

Note that this project will use the double tap to drive the player’s sprint, but it can be applied to anything.

Next create the input buttons we will be using for our double tap event. I am creating an input action named “Sprint” and binding it to the left shift key and the Xbox controller X.

DoubleTap_ProjectSettings

DoubleTap_Input

Next open up the character or pawn which takes this input. In our case it is the Third Person Character. Here we right click and add in the input event we just created.

DoubleTap_SprintEvent

At this point we are ready to create our double tap functionality. Note that I am using collapsed graphs for each of the double tap methods. This isn’t strictly needed but it helps to keep them separate and organized here.

I have created 3 graphs, one for each type of double tap method we will be creating.

DoubleTap_Event

Note that each has 2 inputs for the Sprint Event, the Pressed and Released. Each also has 3 outputs, one for Completed (Released), one for Single Tap, and one for Double Tap.

Again, the outputs are not required as this functionality can take place within the collapsed group if desired. All of my outputs will be hooked up to the CharacterMovement Set MaxWalkSpeed variable which controls our speed.

If you want the same functionality feel free to add that in as well.

Variables

Before we can start we need to setup a few variables. Add in the following:

  • Float
    • Name: PreviousTapTime
  • Float
    • Name: TapThreshold
    • Set to amount of time Double tap must take
      • Currently setup as 0.25 seconds
  • Integer
    • Name: TapNumber
  • Boolean
    • Name: TapHeld
    • If we are currently holding down the input

DoubleTap_Variables

Simple Double Tap

This is a simple way to check for a double tap but does have the draw back of both the single and double being called. Use this if you need something simple and do not have single tap functionality.

DoubleTap_Simple

We first setup a branch to check against the amount of time between our first tap and second. Calculate this by subtracting the our Previous Tap Time from the Real Time Seconds.

Note: Using Real Time Seconds means that this works even if the game is paused or time has been slowed down. Use Game Time if pause and slow motion should effect the time given to complete the double tap.

If the time between taps is less than or equal to our threshold we follow the True path. If not, we follow false and set our PreviousTapTime to the current so that on the next tap we compare correctly.

Finally, hook up the outputs to the speed changes or implement the desired double tap effect here.

Timer Double Tap

This method is more complex but still quite easy to read and understand.

DoubleTap_Timer

First we set our TapHold boolean, one for the pressed and one for the released. Make sure to set them properly by checking the pressed and leaving the released false.

Next we add in two set timers, hooking them up as shown. Within the function name of the “pressed path” timer type TapTimer. After this add the ClearTimer and connect is as shown. In it’s function name type ReleaseTimer.

Within the released path set the timer function to ReleaseTimer.

For each of the SetTimers connect our TapThreshold variable to the time input.

Timers

The way timers work within Unreal is straight forward. By setting a timer you begin a countdown of x amount of seconds. When this timer hits 0 it will search for the function or event name given in it’s “Function Name” input. If it finds one, that event will be called.

However, if a timer is called again before it hits zero, it will stop and restart, not calling the input function until it counts down again.

Clearing the ReleaseTimer is used so that when we let go of the sprint button to perform the double tap it does not call the release code. This code is only called if we do not tap again before the timer has completed.

Increment Number

Instead of just using the previous and current time variables we use a TapNumber variable and increment it.

Add in the increment code as shown. We get our TapNumber variable, add one to it, clamp it, and set it.

The clamp is setup to make sure we are either at 1 or 2 taps and never anything else at this stage. It will be reset to zero later but during a tap it should only ever be 1 or 2.

If you would like to implement a triple tap, or more, increase the clamp here and add to the switch we will create next.

Custom Events

Next we need to create our timer events. Right click and create two custom events naming them ReleaseTimer and TapTimer. These are the events that will be called when our timers reach zero.

If we hit the Release timer first we will reset our TapNumber to zero and move into the switch. If the TapTimer instead is called we move through the switch with the number of taps we currently have.

If we pass through either the 1 or 2 output on the switch, zero out our TapNumber variable for next time and check our Hold boolean. If the player has released the button before the timer hit zero, the bool will be false and they should not sprint.

If your functionality does not need to check for a hold, simply remove the variable and branch. That will let the code pass through here and cause the single or double tap.

Again, I am calling Print Strings and using outputs to connect to my character speed. You can omit the print strings and add your own functionality right in the collapsed graph here.

Delay Double Tap

The delay version is like our Timer version above. The main difference is that this can be easier to move to other functions or reuse the code.

DoubleTapDelay

We start off the same way setting our TapHold variable but then use Sequences to drive a few branches of code. These branches are all called during the same frame one after the other.

We increment and begin a delay on the pressed path. We also close the gate at the bottom so our released delay cannot pass through if it is currently counting down.

This is just like our clear timer function from earlier. Released code does not get called if we tap again in time.

At the bottom you can see we use the sequence to open the gate so when the delay completes it can pass. We released so it is opened until we tap again.

Retriggerable Delay

Be sure to use retriggerable delays instead of normal delays. This is because normal delays do not reset if they are called a second time before completed. They will continue to countdown. Using the retriggerable version allows us to stop the delay and restart it each time we tap.

Integer Switch

Finally we hit the switch again which is the same as the previous version. Place the functionality you need here and you are all set!

Review

We should have 3 working double tap checks. Feel free to use the one that works best for you!

You can also download my character file with the double tap code here: https://drive.google.com/file/d/0B3EStrU6T3QBZlhvaG53N1VqQ2s/view?usp=sharing

Cheers!

 

Categories: Game Design | Tutorials

4 Responses to “Unreal Engine 4: Let’s Make a Double Tap Event”

  1. Hi, thanks for sharing, I’m making an app for samsung gear, with a character in first person, I would like to achieve that with the touch panel in the gear will control the speed of the character, or just make it stop and move forward, I already put in the imput Touch 3, but I do not know how to connect it in the blueprint, nor how to make it recognize as a tap.

    Thanks for you time

Leave a Reply

Your email address will not be published. Required fields are marked *