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

Question Dashing Mechanic

Discussion in 'Scripting' started by B4g3lz, Sep 14, 2021.

  1. B4g3lz

    B4g3lz

    Joined:
    Aug 23, 2020
    Posts:
    7
    Hello! I've been following a tutorial on youtube on how to script a dashing mechanic into my 2D platformer. I followed along with the guide and the youtuber made it so that you have to double tap to dash. I don't want that. I want to make it so that you can tap the dash button once but don't know how to do it. Please help!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DoubleJump : MonoBehaviour
    6. {
    7.    public float speed = 10f;
    8.    public float jumpPower = 20f;
    9.    public int extraJumps = 1;
    10.    [SerializeField] LayerMask groundLayer;
    11.    [SerializeField] Rigidbody2D rb;
    12.    [SerializeField] Transform feet;
    13.  
    14.    int jumpCount = 0;
    15.    bool isGrounded;
    16.    float mx;
    17.    float jumpCoolDown;
    18.  
    19.    public float dashDistance = 15f;
    20.    bool isDashing;
    21.    float tapTime;
    22.    KeyCode lastKeyCode;
    23.  
    24.    private void Update()
    25.    {
    26.        mx = Input.GetAxisRaw("Horizontal");
    27.  
    28.        if (Input.GetButtonDown("Jump"))
    29.        {
    30.            Jump();
    31.        }
    32.  
    33. // Dashing
    34.        if (Input.GetKeyDown(KeyCode.X))
    35.        {
    36.            if (tapTime > Time.time && lastKeyCode == KeyCode.X)
    37.            {
    38.                StartCoroutine(Dash(1f));
    39.            }
    40.            else
    41.            {
    42.                tapTime = Time.time + 0.3f;
    43.            }
    44.  
    45.            lastKeyCode = KeyCode.X;
    46.        }
    47.  
    48.        CheckGrounded();
    49.    }
    50.  
    51.    private void FixedUpdate()
    52.    {
    53.        if (!isDashing)
    54.        rb.velocity = new Vector2(mx * speed, rb.velocity.y);
    55.    }
    56.  
    57.    void Jump()
    58.    {
    59.        if (isGrounded || jumpCount < extraJumps)
    60.        rb.velocity = new Vector2(rb.velocity.x, jumpPower);
    61.        jumpCount++;
    62.    }
    63.  
    64.    void CheckGrounded()
    65.    {
    66.        if (Physics2D.OverlapCircle(feet.position, 0.5f, groundLayer))
    67.        {
    68.            isGrounded = true;
    69.            jumpCount = 0;
    70.            jumpCoolDown = Time.time + 0.2f;
    71.        }
    72.        else if (Time.time < jumpCoolDown)
    73.        {
    74.            isGrounded = true;
    75.        }
    76.        else
    77.        {
    78.            isGrounded = false;
    79.        }
    80.    }
    81.  
    82.    IEnumerator Dash (float direction)
    83.    {
    84.        isDashing = true;
    85.        rb.velocity = new Vector2(rb.velocity.x, 0f);
    86.        rb.AddForce(new Vector2(dashDistance * direction, 0f), ForceMode2D.Impulse);
    87.        float gravity = rb.gravityScale;
    88.        rb.gravityScale = 0;
    89.        yield return new WaitForSeconds(0.4f);
    90.        isDashing = false;
    91.        rb.gravityScale = gravity;
    92.    }
    93. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Might work if you remove all the extra chuff inside from line 35 to line 46 and instead ONLY do line 38 when you press X. It's worth a try.
     
    B4g3lz likes this.
  3. B4g3lz

    B4g3lz

    Joined:
    Aug 23, 2020
    Posts:
    7
    It worked thanks!
     
    Kurt-Dekker likes this.