- Home /
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?
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?
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.
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.
WOW. Your code works like sorcery, no surge at all how is it different to addForce?
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
![](https://koobas.hobune.stream/wayback/20220612162750im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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