Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 nhono99 · May 16, 2018 at 07:02 AM · physicsrigidbodyrigidbody.addforce

Sudden surge of force applied to a rigidbody from continous jumping.

         void Jump(){
         rb.velocity = Vector3.zero;
         rb.angularVelocity = Vector3.zero;
         rb.velocity.Set(0, 0, 0);
         rb.angularVelocity.Set(0, 0, 0);
     
         rb.AddForce(new Vector3(Random.Range(-1,1), 1, 0).normalized * power);
         }
         void Update(){
              if (Input.GetMouseButtonDown(0) && !mouseDown){
               mouseDown = true;
               Jump();
              }
              if (Input.GetMouseButtonUp(0) && mouseDown){
              mouseDown = false;
          }
         }

*This code is called at random time from 0.2seconds to 2seconds to make random movement but it sometimes speed up.

I tried guessing how prevent the rigidbody surging force whenever this code is called but it always happened even I have set the project Maximum Allowed Time to Fixed Time Step. What I've done wrong? Where is the problem?

Comment
Add comment · Show 2
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 Captain_Pineapple · May 16, 2018 at 07:58 AM 0
Share

Can you post a bit more about your code? Where do you call the given code example? Do you only execute this once on Button Press or on collider enter?

avatar image nhono99 Captain_Pineapple · May 16, 2018 at 08:47 AM 0
Share

I've updated it thanks! I'm wondering if this is just the natural behavior of the rigibody's physics. Because I tried Invoking Jump() every 0.5seconds and it returns the same speed no surging or when there is a surge it always surge with the specified repeat time given the same velocity. I even tried the answer here speed limiter but it does not solve my problem.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Captain_Pineapple · May 16, 2018 at 08:58 AM

So far i can not tell you exactly why this behaviour exists but perhaps i can give you another way to get your desired behaviour.

First up the function Input.GetMouseButtonDown(0)already only get's called when you actually press the button down. So unless you don't need the mouse status otherwise, you can safley remove it from your code.

Secondly you can just go ahead an instead of adding a force just directly set a velocity for your rigidbody. Something like this:

    void Jump(){
          rb.velocity = new Vector3(Random.Range(-1,1), 1, 0).normalized * power);
          rb.angularVelocity = Vector3.zero;
     }

you probably want to adjust the power value to this new code since depending on the objects mass it might be too large to be applied as velocity.

Comment
Add comment · 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 nhono99 · May 16, 2018 at 10:49 AM 0
Share

WOW. Your code works like sorcery, no surge at all how is it different to addForce?

avatar image Captain_Pineapple nhono99 · May 16, 2018 at 11:58 AM 1
Share

Well what you did was to force the body to stop by setting the velocity to zero. Then you used addforce to apply motion to the body again. Normally this works fine but sometimes the physics part of unity does not like that much direct fiddling with rigidbody parameters so setting the velocity directly is a fast/easier way.

$$anonymous$$ostly it is recommended to use addforce in the fixedUpdate function wich gets executed just before the next internal physics update. Especially when you want to apply a constant force to an object. In this case you also want to scale the force by using Time.deltTime as a multiplicator.

But as i said: Normally the code you provided should work as planned.

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

160 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 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

Setting a RigidBody's velocity messes with my custom gravity, not sure how to proceed. 0 Answers

Force required to throw an object. 1 Answer

How To Set Individual Rigidbody Gravity [Solved] 3 Answers

Any way to gracefully detach object with Unity physics? 2 Answers

Moving rigidbody (Player) with addForce or Velocity ? 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