- Home /
Rigid body speed not increasing.
I've got a simple scene set up with a ball with a rigid body attached. When the ball rolls down a slope the speed stays at a constant and doesn't increase and pick up speed over time as it rolls as you assume it would. This is the only piece of code I have attached to it which simply moves it forward.
if(Input.GetKey("w")){
rigidbody.AddForce(Vector3.forward * 10);
}
Is there a simple answer to how to get it to increase in speed based on the enviroment.
There're some things that might interfere. Frictional forces of the ground, and Drag property of the RigidBody comes to $$anonymous$$d.
Answer by TheDDestroyer12 · Sep 17, 2014 at 02:53 PM
You shouldn't even have to do that. The ball should roll down the slope as soon as you just have a rigidbody attached to it. I don't know why it don't work like that, but to get that code working, you should place it in the update method, like this:
void Update()
{
if(Input.GetKey(KeyCode.W))
{
rigidbody.AddForce(0, 0, 10);
}
}
That's it. It should work! Good luck!
/TheDDestroyer12
I have it in a properly formatted script, I just pasted the part which handles the rigid body... Also it does roll down the slope, it just doesn't increase it's speed as it rolls.
Physically based code should go in the FixedUpdate routine. Since thats what FixedUpdate is made for.
@Ebil Okay, didn't actually know that. Thanks!
@spunktrumpet By saying it is in a properly formatted script, that doesn't mean you placed it in the (fixed)update function. You don't actually write that in your question, which makes it hard to know. However, if you did, it's weird that it doesn't work, because I wrote down that code (almost. I wrote "0, 0, 10", ins$$anonymous$$d of "Vector3.forward*10". You could try doing like that) and tested it earlier today, before I posted my answer, and it worked! I don't understand what's wrong. You don't get any errors or warnings?
/TheDDestroyer12
Your answer
Follow this Question
Related Questions
jump script : 2D 1 Answer
A node in a childnode? 1 Answer
Errors with gravity switching... 1 Answer
Movement problem? 0 Answers
How can i make artificial gravity for Kinematic Rigidbody? 3 Answers