- Home /
need tutorials for rigidbody.Addforce
hi, iam making a "racing" game and after finishing the controls, ive realised that when moving stuff by transform.translate or rigidbody.velocity, the obejcts dont behave right in terms of physics. After testing it some more I found out that physics work great with moving objects using rigidbody.addforce. The problem is that using playerrb.AddForce ( -player.forward*speed*accspeed*200* Time.deltaTime,ForceMode.Acceleration);
the car doesnt move at all at first, then accelerates really fast and ends up moving at thousands of units per second. Does anyone know any good tutorials for working with "addforce" where I could learn how to solve this problem?
Answer by wibble82 · Mar 09, 2014 at 11:35 AM
Wow - that's a lot of numbers you've plugged in there! :)
Ok, a rough tutorial on forces...
First up, you're right about having to switch to AddForce. Once something is under the control of the physics engine (because it has a none-kinematic rigid body) you need to talk to it in physics language if you want to change how an object moves. Otherwise you're just fighting the physics for control of the object.
There are 2 types of change in velocity that can be applied in physics:
a linear change, which affects rigidbody.velocity. This is applied using AddForce
an angular change, which affects rigidbody.angularVelocity and changes the spin of the object. Applied using AddTorque.
In addition, there is AddForceAtPoint, which applies a force at a position in world space to an object. This has the effect of poking an object with your finger. Depending on where you poke it the obect may start to spin in addition to move. It's very handy for simulating things like collisions, but less useful for driving forces.
Now, all AddForce is doing is changing the velocity of your rigid body slightly. Nothing more. It's really just a slightly more complex way of saying:
rigidbody.velocity += some_acceleration
However, if you look at the docs you'll see AddForce can take a force mode paramter that tells unity what sort of force you want to apply. This can be:
ForceMode.Force (default). A classic force in newtons that you would have learnt about in school. The actual effect on the objects velocity is: my_force*time_step/body_mass. In other words the change in speed caused by this force takes into account both the current time step AND the body's mass. Good for things like racing games where you want to apply a continuous force over multiple frames.
ForceMode.Impulse. 1 level up from a force, this still takes into account mass but ignores time step. The effect on velocity is my_force/body_mass. Good for stuff like explosions or impacts where you just want to apply a 1-shot change in velocity, but still realistically account for the fact that heavy objects respond less to a given force.
ForceMode.Acceleration. A cheap and nasty but sometimes useful direct change in velocity of your object. Doesn't take into account mass or time step. Good for stuff like gravity, where the acceleration is constant regardless of the mass of the object. Or just dirty tweaks to velocity when you can't be bothered to do it properly!
Exactly the same set of things exist for Torque, which changes angular velocity instead.
Finally, before moving on, you might have noticed there is also AddRelativeForce and AddRelativeTorque. These behave just like their counterparts, except the forces are in local space of the object instead of world space. This again is quite handy for stuff like car games, as:
mybody.AddForce(new Vector3(0,0,100)) // adds a force along the world z axis
mybody.AddRelativeForce(new Vector3(0,0,100)) //adds a force that pushes mybody forwards in whatever direction its facing
Your calculations look quite funky to be honest - you seem to be adding an acceleration to the vehicle that is based on the current speed and another value called accspeed and the time step. If my understanding is correct, this means the size of the force you apply is proportional to the speed the object is already moving at, which would cause an exponential curve like the one you describe.
If you're just trying to move your car, probably:
playerrb.AddRelativeForce(new Vector3(0,0,a_force));
Will push the car forwards by a_force. How you calculate that depends on your game of course.
You could use playerrb.AddRelativeTorque(new Vector3(0,my_steering,0)) to give steering - a rotation around the local y axis
Note as well - any of this stuff should be done inside the FixedUpdate function, and look at Time.fixedTime or Time.fixedDeltaTime. The physics runs at a fixed time step once per fixed update, and your logic should run in tandem with the physics engine, not the renderer.
Hope that helps!
Thats the best answer to anything i have ever seen! Thank you! I was just fooling around with that script, there are so many possibilities of how this situation can be handled that i did not know which one to choose. again, thank you! :)
No probs - I used to write car games for a living and like em quite a bit :) If you ever want to get more realistic, the key things to look at are:
ray casts - doing a ray cast from where each wheel is downwards tells you where the ground is relative to your tyre, and thus if you're touching the ground
GetPointVelocity - gets the velocity of a given point on a rigid body. You can use this to find out the velocity of the car at the point the tyres touch the ground
AddPointForce once you know where your tyres touch the ground, and how fast they are moving relative to the ground, you can calculate realistic steering, braking and accelerating forces to apply to the base of each wheel
The simplest way to simulate a car is to make each wheel not collidable, and ins$$anonymous$$d treat it more like a 'hover craft'. You apply a force at each axle that makes the vehicle hover. That force obeys the laws that springs do, so you end up with suspension. Add in those point forces for steering etc and you get a neat feeling car.
That's basically how you get closer to 'realistic' car physics, by which I dont mean simulations, but more stuff that naturally gives you things like drift, hand brake turns, traction etc.
Again, thanks. $$anonymous$$aking the vehicle hover sounds quite interesting, i might do that the next time. The game iam working on currently isnt supposed to be realistic at all in terms of handling and such, i just need some proper reactions when vehicles collide.
Your answer
Follow this Question
Related Questions
Car - WheelCollider - Why the side is getting up? 2 Answers
Applying proper drag and center of mass for a vehicle 1 Answer
Question about rigidbody velocity 1 Answer
How do I add force To rigidbody based On Character's Walking direction? 0 Answers
How to make rigidbodies on each side of a cube fall towards the cube? [multiple gravity / addForce] 0 Answers