- Home /
Double Tap Run
I want to make code the player walks faster if either key A or D is tapped twice. A (being left) and D (being right). Any suggestions on how I can do this. Here’s my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Indingo : MonoBehaviour {
private Rigidbody2D MyRigidBody;
private Animator MyAnimator;
[SerializeField]
private float movementSpeed;
private bool facingRight;
// Use this for initialization
void Start ()
{
facingRight = true;
MyRigidBody = GetComponent();
MyAnimator = GetComponent();
}
// Update is called once per frame
void FixedUpdate ()
{
float horizontal = Input.GetAxis(“Horizontal”);
HandleMovement(horizontal);
Flip(horizontal);
}
private void HandleMovement(float horizontal)
{
MyRigidBody.velocity = new Vector2(horizontal * movementSpeed, MyRigidBody.velocity.y); // x – 1, y = 0;
MyAnimator.SetFloat(“speed”, Mathf.Abs(horizontal));
}
private void Flip(float horizontal)
{
if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
{
facingRight = !facingRight;
Vector3 thescale = transform.localScale;
thescale.x *= -1;
transform.localScale = thescale;
}
}
}
Answer by jordstar20001 · Jul 30, 2017 at 01:41 AM
Hi there @DiamondMC102 . You could simply start a coroutine that waits a certain amount of time (you could have a public float that would represent the max interval between double clicks.
Example:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour {
public float intervalOfDoubleTap;
public float fastMoveSpeed = 4f;
public float slowMoveSpeed = 2f;
bool waitingForDoubleTap_Left = false;
bool waitingForDoubleTap_Right = false;
bool normal_isMovingLeft = false;
bool fast_isMovingLeft = false;
bool normal_isMovingRight = false;
bool fast_isMovingRight = false;
private void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
if (waitingForDoubleTap_Left)
{
normal_isMovingLeft = false;
fast_isMovingLeft = true;
}
waitingForDoubleTap_Left = true;
normal_isMovingLeft = true;
StartCoroutine(WaitForDoubleTap_Left());
}
if (Input.GetKeyDown(KeyCode.D))
{
if (waitingForDoubleTap_Right)
{
normal_isMovingRight = false;
fast_isMovingRight = true;
}
waitingForDoubleTap_Right = true;
normal_isMovingRight = true;
StartCoroutine(WaitForDoubleTap_Right());
}
if (Input.GetKeyUp(KeyCode.A))
{
normal_isMovingLeft = false;
fast_isMovingLeft = false;
}
if (Input.GetKeyUp(KeyCode.D))
{
normal_isMovingRight = false;
fast_isMovingRight = false;
}
}
private void FixedUpdate()
{
if (normal_isMovingLeft)
{
gameObject.transform.Translate(-1 * transform.right * Time.fixedDeltaTime * slowMoveSpeed);
}
if (fast_isMovingLeft)
{
gameObject.transform.Translate(-1 * transform.right * Time.fixedDeltaTime * fastMoveSpeed);
}
if (normal_isMovingRight)
{
gameObject.transform.Translate(transform.right * Time.fixedDeltaTime * slowMoveSpeed);
}
if (fast_isMovingRight)
{
gameObject.transform.Translate(transform.right * Time.fixedDeltaTime * fastMoveSpeed);
}
}
private IEnumerator WaitForDoubleTap_Left()
{
yield return new WaitForSecondsRealtime(intervalOfDoubleTap);
waitingForDoubleTap_Left = false;
}
private IEnumerator WaitForDoubleTap_Right()
{
yield return new WaitForSecondsRealtime(intervalOfDoubleTap);
waitingForDoubleTap_Right = false;
}
}
Repeat the process for forward movement if required.
Go Ahead and drop it on your gameobject, but make sure you fill the values in the inspector.
If this helped you please mark it as the best answer :D
Happy coding!
thanks so much Jordstar, this is really helpful and easy to edit and implement.
Answer by LightWell · Jul 29, 2017 at 04:58 AM
First off this: Using code tags properly
IEnumerator checkIfDoubleTap(float timeInMS, KeyCode keyCode)
{
float timer = 0;
timer += Time.deltaTime;
while (timer < timeInMS)
{
print(timer);
if (Input.GetKeyDown(keyCode))
{
hasSBeenPressed = true;
}
else
{
hasSBeenPressed = false;
}
yield return null;
}
yield return null;
}
This is what I came up for you, you could make this better by having the Ienumerator return a value so you don't manually set whether the k had been pressed. I called this with this:
if (Input.GetKeyDown(KeyCode.S))
{
StartCoroutine(checkIfDoubleTap(200, KeyCode.S));
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612131432im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Double tap on iPhone very unreliable 2 Answers
Double-tap Number keys 1 Answer
[SOLVED] Walk, Run and Sprint with single key 1 Answer
How to make double buttons? 3 Answers
When i double Tap single Tap function also Executing.. 1 Answer