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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Bug Dashing multiple times in Unity when not intended

Discussion in 'Scripting' started by CarterAWebb, Jan 3, 2023.

  1. CarterAWebb

    CarterAWebb

    Joined:
    Dec 30, 2022
    Posts:
    2
    Hi there! I am currently making a 2d platformer game and I was trying to implement an ability to dash once in any direction if the play has touched the ground or if they havent used their dash yet. Basically, the player gets the ability to dash if they have touched the ground and it doesnt go away until they use their dash. The problem I am having is that only sometimes does my code work. Sometimes it will dash once into the air like its supposed to, but other times I can get it to dash 2, 3, or even 4 times even if I am not touching the ground if I just spam the dash button (space). Could I get some feedback and help on what might be causing this issue? Also, sorry if my code is ugly/disorganized.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     //All objects used
    8.     private Rigidbody2D rb;
    9.     private BoxCollider2D boxCollider;
    10.     [SerializeField] private LayerMask jumpableGround;
    11.     private SpriteRenderer sprite;
    12.     private Animator anim;
    13.  
    14.     //Movement variables
    15.     private float dirX = 0f;
    16.     [SerializeField] private float moveSpeed = 12f;
    17.     [SerializeField] private float jumpForce = 2;
    18.     private readonly float jumpStartTime = 0.2f;
    19.  
    20.     //Dash variables
    21.     private bool canDash = true;
    22.     private bool hasDashed = false;
    23.     private bool isDashing = false;
    24.     private float dashPower = 18f;
    25.     private float dashTime = 0.35f;
    26.     private float dashCooldown = 0.35f;
    27.     private int dashCounter = 1;
    28.     [SerializeField] private TrailRenderer dashTrail;
    29.     private DashDirection dashDirection;
    30.  
    31.     //Jumping variables
    32.     private float jumpTime;
    33.     private bool isJumping = false;
    34.  
    35.     //Coyote time variables
    36.     private readonly float coyoteTime = 0.25f;
    37.     private float coyoteTimeCounter;
    38.  
    39.     //Jump Buffer variables
    40.     private readonly float jumpBufferTime = 0.2f;
    41.     private float jumpBufferCounter;
    42.  
    43.     private enum MovementState { idle, walking, running, jumping, falling, lightAttack, heavyAttack, damage, death, roll, dash }
    44.     private enum DashDirection { left, right, up, down, upLeft, upRight, downLeft, downRight, none }
    45.  
    46.     // Start is called before the first frame update
    47.     private void Start()
    48.     {
    49.         rb = GetComponent<Rigidbody2D>();
    50.         boxCollider = GetComponent<BoxCollider2D>();
    51.         sprite = GetComponent<SpriteRenderer>();
    52.         anim = GetComponent<Animator>();
    53.         dashDirection = DashDirection.none;
    54.     }
    55.  
    56.     // Update is called once per frame
    57.     private void Update()
    58.     {
    59.         /*  ----------  Cancel movement if dashing  ----------  */
    60.         if (isDashing)
    61.         {
    62.             return;
    63.         }
    64.  
    65.         /*  ----------    Initial Set-Up    ----------  */
    66.         dirX = Input.GetAxisRaw("Horizontal");
    67.         rb.velocity = new Vector2(dirX * moveSpeed, rb.velocity.y);
    68.  
    69.         if (IsGrounded())
    70.         {
    71.             coyoteTimeCounter = coyoteTime;
    72.         }
    73.         else
    74.         {
    75.             coyoteTimeCounter -= Time.deltaTime;
    76.         }
    77.  
    78.         if (Input.GetButtonDown("Jump"))
    79.         {
    80.             jumpBufferCounter = jumpBufferTime;
    81.         }
    82.         else
    83.         {
    84.             jumpBufferCounter -= Time.deltaTime;
    85.         }
    86.  
    87.         if (IsGrounded() || (isJumping && !hasDashed))
    88.         {
    89.             canDash = true;
    90.         }
    91.  
    92.         /*  ----------    Y-Axis Movement    ----------  */
    93.         if (jumpBufferCounter > 0 && coyoteTimeCounter > 0f)
    94.         {
    95.             isJumping = true;
    96.             jumpTime = jumpStartTime;
    97.  
    98.             rb.velocity = new Vector2(rb.velocity.x, jumpForce);
    99.             jumpBufferCounter = 0;
    100.         }
    101.  
    102.         /*  ----------    Variable Jumping    ----------  */
    103.         if (Input.GetButton("Jump") && isJumping)
    104.         {
    105.             if (jumpTime > 0)
    106.             {
    107.                 rb.velocity = Vector2.up * 0.9f * jumpForce;
    108.                 jumpTime -= Time.deltaTime;
    109.             }
    110.             else
    111.             {
    112.                 isJumping = false;
    113.             }
    114.         }
    115.  
    116.         if (Input.GetButtonUp("Jump"))
    117.         {
    118.             isJumping = false;
    119.             coyoteTimeCounter = 0;
    120.         }
    121.  
    122.         /*  ----------    Dashing    ----------  */
    123.         if (Input.GetAxisRaw("Horizontal") < 0)
    124.         {
    125.             dashDirection = DashDirection.left;
    126.         }
    127.         else if (Input.GetAxisRaw("Horizontal") > 0)
    128.         {
    129.             dashDirection = DashDirection.right;
    130.         }
    131.  
    132.         if (Input.GetAxisRaw("Vertical") < 0)
    133.         {
    134.             dashDirection = DashDirection.down;
    135.         }
    136.         else if (Input.GetAxisRaw("Vertical") > 0)
    137.         {
    138.             dashDirection = DashDirection.up;
    139.         }
    140.  
    141.         if (Input.GetAxisRaw("Horizontal") < 0 && Input.GetAxisRaw("Vertical") < 0)
    142.         {
    143.             dashDirection = DashDirection.downLeft;
    144.         }
    145.         else if (Input.GetAxisRaw("Horizontal") > 0 && Input.GetAxisRaw("Vertical") < 0)
    146.         {
    147.             dashDirection = DashDirection.downRight;
    148.         }
    149.  
    150.         if (Input.GetAxisRaw("Horizontal") < 0 && Input.GetAxisRaw("Vertical") > 0)
    151.         {
    152.             dashDirection = DashDirection.upLeft;
    153.         }
    154.         else if (Input.GetAxisRaw("Horizontal") > 0 && Input.GetAxisRaw("Vertical") > 0)
    155.         {
    156.             dashDirection = DashDirection.upRight;
    157.         }
    158.  
    159.         if (Input.GetButtonDown("Dash") && canDash)
    160.         {
    161.             StartCoroutine(Dash(dashDirection));
    162.         }
    163.  
    164.         //UpdateAnimationState();
    165.     }
    166.  
    167.     private void UpdateAnimationState()
    168.     {
    169.         MovementState state;
    170.  
    171.         if (dirX > 0f)
    172.         {
    173.             if (Input.GetButtonDown("Sprint"))
    174.             {
    175.                 state = MovementState.running;
    176.                 sprite.flipX = false;
    177.             }
    178.             else
    179.             {
    180.                 state = MovementState.walking;
    181.                 sprite.flipX = false;
    182.             }
    183.         }
    184.         else if (dirX < 0f)
    185.         {
    186.             if (Input.GetButtonDown("Sprint"))
    187.             {
    188.                 state = MovementState.running;
    189.                 sprite.flipX = true;
    190.             }
    191.             else
    192.             {
    193.                 state = MovementState.walking;
    194.                 sprite.flipX = true;
    195.             }
    196.         }
    197.         else
    198.         {
    199.             state = MovementState.idle;
    200.         }
    201.  
    202.         if (rb.velocity.y > 0.1f)
    203.         {
    204.             state = MovementState.jumping;
    205.         }
    206.         else if (rb.velocity.y < -0.1f)
    207.         {
    208.             state = MovementState.falling;
    209.         }
    210.        
    211.         anim.SetInteger("state", (int)state);
    212.     }
    213.  
    214.     private bool IsGrounded()
    215.     {
    216.         return Physics2D.BoxCast(boxCollider.bounds.center, boxCollider.bounds.size, 0f, Vector2.down, 0.1f, jumpableGround);
    217.     }
    218.  
    219.     private IEnumerator Dash(DashDirection dD)
    220.     {
    221.         //Make a dash function that allows the player to dash in the direction they are directing their movement
    222.         isDashing = true;
    223.         canDash = false;
    224.         dashTrail.emitting = true;
    225.         hasDashed = true;
    226.         float originalGravity = rb.gravityScale;
    227.         rb.gravityScale = 0f;
    228.         dashCounter = 1;
    229.  
    230.         if (dD == DashDirection.left)
    231.         {
    232.             rb.velocity = new Vector2(-dashPower, 0f);
    233.         }
    234.         else if (dD == DashDirection.right)
    235.         {
    236.             rb.velocity = new Vector2(dashPower, 0f);
    237.         }
    238.         else if (dD == DashDirection.up)
    239.         {
    240.             rb.velocity = new Vector2(0f, dashPower);
    241.         }
    242.         else if (dD == DashDirection.down)
    243.         {
    244.             rb.velocity = new Vector2(0f, -dashPower);
    245.         }
    246.         else if (dD == DashDirection.upLeft)
    247.         {
    248.             rb.velocity = new Vector2(-dashPower, dashPower);
    249.         }
    250.         else if (dD == DashDirection.upRight)
    251.         {
    252.             rb.velocity = new Vector2(dashPower, dashPower);
    253.         }
    254.         else if (dD == DashDirection.downLeft)
    255.         {
    256.             rb.velocity = new Vector2(-dashPower, -dashPower);
    257.         }
    258.         else if (dD == DashDirection.downRight)
    259.         {
    260.             rb.velocity = new Vector2(dashPower, -dashPower);
    261.         }
    262.  
    263.         yield return new WaitForSeconds(dashTime);
    264.  
    265.         isDashing = false;
    266.         dashTrail.emitting = false;
    267.         rb.gravityScale = originalGravity;
    268.         hasDashed = false;
    269.  
    270.         yield return new WaitForSeconds(dashCooldown);
    271.     }
    272. }
     
  2. CarterAWebb

    CarterAWebb

    Joined:
    Dec 30, 2022
    Posts:
    2
    Hi, after looking at my code some more, I added a dash counter that just stops dashing if it has happened more already and you havent touched the ground, and now it works!
     
    Kurt-Dekker likes this.