• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Lainex · Feb 24, 2015 at 06:20 AM · movementrigidbody2dvelocityaddforcedash

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 t$$anonymous$$nking about implementing das$$anonymous$$ng 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; //T$$anonymous$$s 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;
     }

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by melkorinos · Feb 24, 2015 at 09:29 AM

You can use AddForce with a force mode, i would try impulse.

Comment

People who like this

0 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lainex · Feb 24, 2015 at 03:50 PM 0
Share

That still doesn't seem to work. It is still being cancelled out by the horzintal user input.

avatar image melkorinos · Feb 24, 2015 at 04:23 PM 0
Share

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 = Mathf.Lerp (dashSpeed, HorizontalSpeed , dashTime);

play around with the values to achieve what you want, e.g change dashTime to Time.something values.

avatar image

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 anyt$$anonymous$$ng like mine, but I disable all other player actions for the duration of the dash. I have float dashDuration w$$anonymous$$ch designates how long I want the dash to last.

so after you do that, somet$$anonymous$$ng like t$$anonymous$$s would work:

 if(!isDas$$anonymous$$ng){
       rigidbody2D.velocity = movement
 }
Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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

I need Help with AdForce pls :( 1 Answer

Unity 2D addforce problem 1 Answer

Stuttering/jerky movement of kinematic rigidbody 2d moving by velocity in 2D game on iOS 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges