Search Unity

Help with dashes (using ridigbody 2D)

Discussion in 'Scripting' started by petizero, Mar 27, 2020.

  1. petizero

    petizero

    Joined:
    Jan 1, 2019
    Posts:
    47
    Hi! So i am creating a small game and im trying to get used to rigidbody movement currently i use .velocity to move around (i am aware of addforce and movePosition) But when i'm trying to do a dash it just won't do anything despite me trying to play with the numbers. any help?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ImprovedMove : MonoBehaviour
    6. {
    7.     public float speed;
    8.     public Rigidbody2D rb;
    9.  
    10.     private float movementH;
    11.    
    12.     ///Dash
    13.  
    14.     private float lastDTime;
    15.    
    16.  
    17.     void Start()
    18.     {
    19.      
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         movementH = Input.GetAxis("Horizontal");
    26.  
    27.         if (Input.GetKeyDown(KeyCode.Space))
    28.         {
    29.             jump();
    30.  
    31.  
    32.         }
    33.  
    34.         if (Input.GetKeyDown(KeyCode.D))
    35.         {
    36.             float timeSinceLastD = Time.time - lastDTime;
    37.             lastDTime = Time.time;
    38.            
    39.             if(timeSinceLastD <= 0.2f)
    40.             {
    41.                
    42.  
    43.  
    44.  
    45.             }
    46.         }
    47.  
    48.  
    49.     }
    50.  
    51.     void FixedUpdate()
    52.     {
    53.         rb.velocity = new Vector2(movementH * speed, rb.velocity.y);  
    54.     }
    55.  
    56.     void jump()
    57.     {
    58.         rb.velocity = new Vector2(movementH * speed, rb.velocity.y + 10);
    59.     }
    60.  
    61. }
    62.  
     
  2. petizero

    petizero

    Joined:
    Jan 1, 2019
    Posts:
    47
    I forgot to include the part in if(timeSinceLastD <= 0.2f)
    but it was just a velocity increased on x
     
  3. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    493
    Please paste in the entire script. No need to remove parts out of it.
     
  4. petizero

    petizero

    Joined:
    Jan 1, 2019
    Posts:
    47
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ImprovedMove : MonoBehaviour
    6. {
    7.     public float speed;
    8.     public Rigidbody2D rb;
    9.  
    10.     private float movementH;
    11.    
    12.     ///Dash
    13.  
    14.     private float lastDTime;
    15.    
    16.  
    17.     void Start()
    18.     {
    19.      
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         movementH = Input.GetAxis("Horizontal");
    26.  
    27.         if (Input.GetKeyDown(KeyCode.Space))
    28.         {
    29.             jump();
    30.  
    31.  
    32.         }
    33.  
    34.         if (Input.GetKeyDown(KeyCode.D))
    35.         {
    36.             float timeSinceLastD = Time.time - lastDTime;
    37.             lastDTime = Time.time;
    38.            
    39.             if(timeSinceLastD <= 0.2f)
    40.             {
    41.  
    42.                 rb.velocity = new Vector2(10 * movementH * speed, rb.velocity.y);
    43.  
    44.  
    45.             }
    46.         }
    47.  
    48.  
    49.     }
    50.  
    51.     void FixedUpdate()
    52.     {
    53.         rb.velocity = new Vector2(movementH * speed, rb.velocity.y);  
    54.     }
    55.  
    56.     void jump()
    57.     {
    58.         rb.velocity = new Vector2(movementH * speed, rb.velocity.y + 10);
    59.     }
    60.  
    61. }
    62.  
    Yeah sorry about that
     
  5. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    493
    Amm first of all, place a Debug.Log("Test") on line 41 and see if it's ever being called.

    If it is, you might have some issues with rb.velocity overriding each other. Now, if that is the case, I have no idea how it was actually working up until this point. To test out this theory, try commenting line 53 and see if dashing works then.