Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Double Tap to DASH!

Discussion in 'Scripting' started by clayton1314, Aug 5, 2019.

  1. clayton1314

    clayton1314

    Joined:
    Jul 11, 2017
    Posts:
    2
    So I went through a couple of tutorials and made a DoubleTap Dash but there is a problem the dash doesn't reset once the double tap is done running. how do i reset it?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6.  
    7. public class Dashing : MonoBehaviour
    8. {
    9.     private Rigidbody2D rb2d;
    10.     public float dashSpeed;
    11.     private float dashTime;
    12.     public float startDashTime;
    13.     private int direction;
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         rb2d = GetComponent<Rigidbody2D>();
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         if (direction == 0)
    25.         {
    26.             if (Input.GetKeyUp(KeyCode.A))
    27.             {
    28.                 direction = 1;
    29.             }
    30.         }
    31.         else
    32.         {
    33.             if (dashTime <= 0)
    34.             {
    35.                 direction = 0;
    36.                 dashTime = startDashTime;
    37.                 rb2d.velocity = Vector2.zero;
    38.             }
    39.             else
    40.             {
    41.                 dashTime -= Time.deltaTime;
    42.  
    43.                 if (direction == 1)
    44.                 {
    45.                     rb2d.velocity = Vector2.left * dashSpeed;
    46.                 }
    47.                 else if (direction == 2)
    48.                 {
    49.                     rb2d.velocity = Vector2.right * dashSpeed;
    50.                 }
    51.             }
    52.         }
    53.     }
    54. }
    55.  
    56.  

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DoubleTap : Dashing
    6. {
    7.  
    8.     public float tapSpeed = 0.5f; //in seconds
    9.  
    10.     private float lastTapTime = 0;
    11.  
    12.     // Use this for initialization
    13.  
    14.     void Start()
    15.     {
    16.  
    17.         lastTapTime = 0;
    18.  
    19.     }
    20.  
    21.     // Update is called once per frame
    22.  
    23.     void Update()
    24.     {
    25.  
    26.         if (Input.GetKeyDown(KeyCode.A))
    27.         {
    28.  
    29.             if ((Time.time - lastTapTime) < tapSpeed)
    30.             {
    31.  
    32.                 Debug.Log("Double tap");
    33.  
    34.             }
    35.  
    36.             lastTapTime = Time.time;
    37.  
    38.         }
    39.  
    40.     }
    41.  
    42. }