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

dash in 2d platformer

Discussion in 'Scripting' started by gaituyenquang1981, Apr 30, 2020.

  1. gaituyenquang1981

    gaituyenquang1981

    Joined:
    Sep 2, 2019
    Posts:
    5
    so i made a dash script . it work but the player can only dash to the left . i want to dash to the right .pls help


    here is the script


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class dash : MonoBehaviour
    {
    private Rigidbody2D rb;
    public float dashspeed;
    private float dashTime;
    public float startDashTime;
    private int direction;

    void Start()
    {
    rb = GetComponent<Rigidbody2D>();
    dashTime = startDashTime;
    }

    void Update()
    {
    if (direction == 0)
    {
    if (Input.GetKeyDown(KeyCode.LeftShift))
    {
    direction = 1;
    }
    else if (Input.GetKeyDown(KeyCode.RightShift))
    {
    direction = 2;
    }
    }
    else
    {
    if (dashTime < -0)
    {
    direction = 0;
    dashTime = startDashTime;
    rb.velocity = Vector2.zero;
    }
    else
    {
    dashTime -= Time.deltaTime;

    if (direction == 1)
    {
    rb.velocity = Vector2.left * dashspeed;
    }
    else if (direction == 2)
    {
    rb.velocity = Vector2.right * dashspeed;

    }
    }
    }
    }
    }
     
  2. sbalanoff

    sbalanoff

    Joined:
    Nov 14, 2019
    Posts:
    36
    Remove the if(direction == 0) at the beginning of your update and add if(direction != 0) at the start of you if statements for direction == 1 and direction == 2

    Code (CSharp):
    1. public class dash : MonoBehaviour
    2. {
    3.     private Rigidbody2D rb;
    4.     public float dashspeed;
    5.     private float dashTime;
    6.     public float startDashTime;
    7.     private int direction;
    8.  
    9.     void Start()
    10.     {
    11.         rb = GetComponent<Rigidbody2D>();
    12.         dashTime = startDashTime;
    13.     }
    14.  
    15.     void Update()
    16.     {
    17.         if (Input.GetKeyDown(KeyCode.LeftShift)) {
    18.             direction = 1;
    19.         } else if (Input.GetKeyDown(KeyCode.RightShift)){
    20.             direction = 2;
    21.         } else {
    22.             if (dashTime < -0) {
    23.                 direction = 0;
    24.                 dashTime = startDashTime;
    25.                 rb.velocity = Vector2.zero;
    26.             } else {
    27.                 dashTime -= Time.deltaTime;
    28.                 if(direction != 0){
    29.                     if (direction == 1) {
    30.                         rb.velocity = Vector2.left * dashspeed;
    31.                     }
    32.                     if (direction == 2) {
    33.                         rb.velocity = Vector2.right * dashspeed;
    34.                     }
    35.                 }
    36.             }
    37.         }
    38.     }
    39. }