Setting a limit to how much force can be applied to an object?


I’m trying to throw together a simple character controller, building it around a rigidbody and applying forces to it to make it move (I want a character controller that interacts with basic physical elements).

So far I can get it to move fine with

function Update ()
	rigidbody.AddForce(Vector3.forward * Input.GetAxis("Vertical"), ForceMode.Impulse);

which adds whatever the value of the W/S key axis (1 for ‘W’, -1 for ‘S’) as a force. But of course, here there is no limit to the force, and the player can accelerate endlessly if they hold down the movement keys.

After trying to make a sort of force-counter which accumulates once every loop if force is being applied, and stops force from being applied if it reaches 10, I’ve decided that I really need to track decreases in the momentum of my character controller as well as increases. But simply tracking player velocity seems like it might cause a problem; it also sets a limit to how quickly the wind-mechanic of my game can blow the player about. What I need to do is precisely track the force applied from player-input alone, regardless of other forces and the consequent velocity

So I’m a little stumped. Can anyone help?

First issue (not related to your question), is that you should be applying your force in FixedUpdate(). The time between Update() calls varies, so you are not getting a constant application of force.

As for limiting force, the first thing to try is setting the drag in the rigidbody. By default it is 0.0, so you are not getting any drag.

You can also play games limiting velocity with a pseudo drag calculation. Put this in FixedUpdate():

rigidbody.velocity = rigidbody.velocity * 0.995;

Play with different values in the 0.98 to 0.999 range.