Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by CheMBurN · Jan 03, 2021 at 01:49 PM · rigidbody2dvelocitydash

Curious issue - having Run() method locks every other velocity modifier

I've a character that I've written a script to have a run and a dash, but when the run function is included, the dash completely stops working and I have no idea why. Commenting out the Run() bits and viola the the dash works.

Here's a bit of my code:

     float dashVelocity = 40f;
     float deltaX;
         
     Rigidbody2D p_rb;
     Animator p_ani;
     SpriteRenderer p_sr;
     p_rb = GetComponent<Rigidbody2D>();
     p_ani = GetComponent<Animator>();
     p_sr = GetComponent<SpriteRenderer>();   
     
         void Update()
         {

             if (Input.GetButtonDown("Fire3"))
             {
                 
                 p_rb.velocity = transform.right * dashVelocity;
                 Debug.Log("pressed");
     
             }
     
             Run();
     
           }
     
         void Run()
         {        
             deltaX = Input.GetAxis("Horizontal") * moveSpeed;
             p_rb.velocity = new Vector2(deltaX, p_rb.velocity.y);
          }
       }

 

(Sorry about the formatting)

Tried setting the rigidbody2D to interpolate and tried moving functions around between update and fixedUpdated but the issue persists.

Comment
Add comment
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
1
Best Answer

Answer by Zymurer · Jan 08, 2021 at 10:31 AM

There is no condition (if clause) for Run. When you pressed the button to dash. Unity detects both Run and dash. When you pressed fire3 it runs your codes in if for a moment but then runs Run function again.

Try to add a if clause also for Run. (for example if fire3 is not pressed).

Comment
Add comment · 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
1

Answer by Bunny83 · Jan 08, 2021 at 01:44 PM

Well you set your characters velocity every frame inside your Run method. Setting it again in one frame when you press a button does not have any effect at all since your Run method will overwrite the velocity again. A dash usually takes some time. Without the Run method when you set the velocity inside your if statement, the velocity will of course stay. Depending on your friction / drag settings, the object may slow down or just continues moving at the speed you set.


Directly modifying the velocity can not be done from two places. Your dash mechanic currently relies on the state of the rigidbody while your Run mechanic overwrites any physics / forces induced behaviour since you always set the velocity directly.


If you want to preserve your Run behaviour you could add the dash there as a seperate variable. Though you have to take care about your dash timing. The easiest way would be a coroutine like that:

 float dashVelocity = 40f;
 float dashTime = 0.3f;
 float dash;
 Rigidbody2D p_rb;
 
 void Start()
 {
     p_rb = GetComponent<Rigidbody2D>();
     StartCoroutine(HandleDash());
 }
 
 void Update()
 {
     float deltaX = Input.GetAxis("Horizontal") * moveSpeed;
     p_rb.velocity = new Vector2(deltaX + dash, p_rb.velocity.y);
 }
 
 IEnumerator HandleDash()
 {
     while(true)
     {
         if (Input.GetButtonDown("Fire3"))
         {
             dash = dashVelocity;
             yield return new WaitForSeconds(dashTime);
             dash = 0f;
             // implement cooldown if you want by uncommenting the following line
             // yield return new WaitForSeconds(dashCooldown);
         }
         yield return null;
     }
 }
Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

117 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Need help understanding advise 1 Answer

Dashing with rigidbody2D not working right 2 Answers

Rigidbody2D.AddForce can't change X velocity 1 Answer

Keeping momentum with rigid bodies. 0 Answers

Rigidbody2D with interpolation lags while moving by velocity, how to solve the problem? 1 Answer


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