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

Dash

Discussion in 'Scripting' started by Leandre5, May 2, 2020.

  1. Leandre5

    Leandre5

    Joined:
    Nov 1, 2017
    Posts:
    42
    Hello, I explain you my problem, I would like to make a dash, I've watched several videos but I can't find a solution for my code (I dash when the time is not less than 0).

    Code (CSharp):
    1. void Update()
    2.     {
    3.         movement.x = Input.GetAxisRaw("Horizontal");
    4.         movement.y = Input.GetAxisRaw("Vertical");
    5.         LastDirection();
    6.          
    7.      
    8.         if(Input.GetKeyDown(KeyCode.Space)){
    9.             if(dashTime < 0){
    10.                 Dash();
    11.                 dashTime = Timetodash;
    12.             }
    13.         }
    14.         dashTime -= Time.deltaTime;
    15.      
    16.     }
    Code (CSharp):
    1. void LastDirection()
    2.     {
    3.         if(Input.GetKeyDown(KeyCode.Z)){
    4.             direction = 1;
    5.         }else if(Input.GetKeyDown(KeyCode.Q)){
    6.             direction = 2;
    7.         }else if(Input.GetKeyDown(KeyCode.S)){
    8.             direction = 3;
    9.         }else if(Input.GetKeyDown(KeyCode.D)){
    10.             direction = 4;
    11.         }
    12.     }
    Code (CSharp):
    1. void Dash()
    2.     {
    3.         if(direction == 1){
    4.             transform.position += new Vector3(5,0,0);
    5.         }else if(direction == 2){
    6.            transform.position += new Vector3(0,5,0);
    7.         }else if(direction == 3){
    8.            transform.position += new Vector3(-5,0,0);
    9.         }else if(direction == 4){
    10.            transform.position += new Vector3(0,-5,0);
    11.         }
    12.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    In the first code bloack above, put a
    Debug.Log( "dashTime = " + dashTime);
    right before you call Dash() and I am sure you will see it is less than or equal to zero. You might also check what TimeToDash is, because if it is zero, it will let you dash again immediately.
     
  3. Leandre5

    Leandre5

    Joined:
    Nov 1, 2017
    Posts:
    42
    My Timetodash is equal to 10 and yes I know that Dashtime is less than zero but I just don't understand why when it's higher I can dash as well...