- Home /
Dashing with rigidbody2D not working right
So I asked a similar question few days ago regarding jumping, and it lead me to an aswer that I'm actually cancelling out my Y velicity with the part of the script that handles rest of the movement. Now I was thinking about implementing dashing using rigidbody2D.AddForce(Vector3.right * dashSpeed); ,but the code is being cancelled out because the X value ir being updated every frame using values from directional keys, if that makes sense. So can you guys please help me figure out a workaround?
Here's my code:
     public int HorizontalSpeed = 10;
         public float jumpSpeed = 1000f;
         public float dashSpeed = 1000f;
         private Vector2 movement;
         public bool fallin = false;
     
     void Update () {
             //Horizontal movement
             float inputX = Input.GetAxis ("Horizontal");
             movement = new Vector2 (inputX * HorizontalSpeed, rigidbody2D.velocity.y);
     }
     
     void FixedUpdate () {
     
         //Jumping
         if (Input.GetKeyDown(KeyCode.X))
         {
             rigidbody2D.AddForce(Vector3.right * dashSpeed);
         }
     
         //Jumping
         if (Input.GetKeyDown(KeyCode.Space) && fallin == false)
         {
             rigidbody2D.AddForce(Vector3.up * jumpSpeed);
         }
         fallin = true;
     
         rigidbody2D.velocity = movement; //This zeroes out the x value each frame
 // so I can't dash properly. It just blinks instead of being smooth. Workaround?
     }
     
     void OnCollisionStay2D() {
         fallin = false;
     }
Answer by melkorinos · Feb 24, 2015 at 09:29 AM
You can use AddForce with a force mode, i would try impulse.
That still doesn't seem to work. It is still being cancelled out by the horzintal user input.
mm ok . This is a workaround but you could implement a timer along with a variable that is called dashTime which will be how long you want the dash to last. So when dashing you set the timer = Time.time and then in your update you execute the movement command if timer + dashTime> Time.time.
Another thing you could do is to use lerp. So you would set a dashSpeed var and then when dashing you could set a bool true and in your update script you would do
 HorizontalSpeed = $$anonymous$$athf.Lerp (dashSpeed, HorizontalSpeed , dashTime);
play around with the values to achieve what you want, e.g change dashTime to Time.something values.
Answer by brlan10 · Oct 20, 2016 at 01:01 PM
You just need to disable whatever input is interrupting your dash. Idk if your dash is anything like mine, but I disable all other player actions for the duration of the dash. I have float dashDuration which designates how long I want the dash to last.
so after you do that, something like this would work:
 if(!isDashing){
       rigidbody2D.velocity = movement
 }
Your answer
 
 
             Follow this Question
Related Questions
move 2d character affected by physics with velocity and/or add force 2 Answers
[C#, 2D] How do I apply force to a player using vector 3 velocity to move 1 Answer
Moving game object have different speeds in different screen resolutions and it's jittering 1 Answer
Smooth 3D Dodge Roll Animation 0 Answers
Limited movement in the air 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                