Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DiamondMC102 · Jul 29, 2017 at 02:06 AM · double-tap

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;
 }
 }
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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!

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jordstar20001 · Jul 30, 2017 at 01:42 AM 0
Share

Lmk if there are any problems :)

avatar image AnmolXP · Nov 05, 2018 at 11:55 PM 0
Share

thanks so much Jordstar, this is really helpful and easy to edit and implement.

avatar image
0

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));
         }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

70 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges