Search Unity

3d Sidescrolling Dashing Script Issues

Discussion in 'Scripting' started by xRaTiOnX, Dec 3, 2018.

  1. xRaTiOnX

    xRaTiOnX

    Joined:
    Dec 10, 2017
    Posts:
    1
    Hello everyone, I've been having some issues in making my dash script work like I want to for some days. I did some search around the forums about similar issues(which exists) but their solutions don't seen to be very much compatible with what I intend.

    I'm trying to create a smooth dash script for my 2.5d (3d sidescroller) character in Unity, I did finish the dash animation, limit air dash to 1 and also dash on the ground with cooldown.

    The problem is, the dash movement is so fast, it's like the player is almost teleporting. For now I'm having it dash like that but with time slow down every dash, kind of a "roundabout solution" or something like that. For reference, here is a part of my code, the dash function:
    Code (CSharp):
    1. void Dashing() {
    2.         //grounddashing and airdashing
    3.         if (Time.time > nextDash)
    4.             if(isGrounded)
    5.                 canDash = true;
    6.  
    7.         if (isGrounded)
    8.         {
    9.             if(Input.GetButtonDown("Fire3") && canDash)
    10.             {
    11.                 if (facingRight)
    12.                 {
    13.                     rb.velocity = new Vector3(0, 0, 0);
    14.                     rb.AddForce(Vector3.right * dashForce, ForceMode.Impulse);
    15.                     anim.Play("Dash");
    16.                     canDash = false;
    17.                     nextDash = Time.time + dashCooldown;                
    18.                 }
    19.                 if (!facingRight)
    20.                 {
    21.                     rb.velocity = new Vector3(0, 0, 0);
    22.                     rb.AddForce(Vector3.left * dashForce, ForceMode.Impulse);
    23.                     anim.Play("Dash");
    24.                     canDash = false;
    25.                     nextDash = Time.time + dashCooldown;      
    26.                 }
    27.             }                          
    28.         }
    29.         if (!isGrounded)
    30.         {
    31.             if (Input.GetButtonDown("Fire3") && canDash)
    32.             {
    33.                 if (facingRight)
    34.                 {
    35.                     rb.velocity = new Vector3(0, 0, 0);
    36.                     rb.AddForce(Vector3.right * dashForce, ForceMode.Impulse);
    37.                     anim.Play("Dash");
    38.                     canDash = false;
    39.                     nextDash = Time.time + dashCooldown;
    40.                 }
    41.                 if (!facingRight)
    42.                 {
    43.                     rb.velocity = new Vector3(0, 0, 0);
    44.                     rb.AddForce(Vector3.left * dashForce, ForceMode.Impulse);
    45.                     anim.Play("Dash");
    46.                     canDash = false;
    47.                     nextDash = Time.time + dashCooldown;
    48.                 }
    49.             }
    50.         }
    51.     }
    Notes:

    private Animator anim;
    private Rigidbody rb;

    My character uses a rigidbody and his entire movement is with a rigidbody, I made the dash movement with rigidbody.AddForce and ForceMode.Impulse, for some reason it works perfectly with the jump (meaning that the jump is not like a teleport), which I coded the same way, but unfortunately the same case cannot be said for my dash movement.

    I've tried using other forms of movement using transform, but using it this way not only it also teleports but the character doesn't collide with objects here. I've also tried putting the Dashing function on both Update and FixedUpdate but the results are the same.

    If anybody could help me on this, I would very much appreciate it.
     
    Last edited: Dec 3, 2018