controlling a rigidbody

What do I do wrong with this? Sometimes the rigid body shots away at too high of velocity. i have got up to 18-19 in velocity even when my max is 10.

Think I missed some error in this match. There is some debug code in this as well to look at the numbers and all seems right all but the velocity. maby there is one thing sometimes the speedDiffFract gets negative is should be always between 0-1.0f.

	public float maxSpeed = 5f;
	public Vector3 inputDirection;
	public float acceleration = 20f;
	public float maxSlope = 40f;
	public float airDamp = 0.1f;
	public float currentSpeed;
	public bool grounded = false;
	private float mass;
	public Vector3 velocity;
	public Vector3 upVector;
	public Vector3 correctedAddForce;
	private float massAcceleration;


	public float speedDiffFract;
	public float forceDot;
	public Vector3 modifiedForce;
	public Vector3 correctedForce;

	// temp data
	public float maxModForce = 0f;
	public float minModForce = 0f;
	public float minVelocity = 0f;
	public float maxVelocity = 0f;
	public float maxWantAdd = 0f;
	public float minWantAdd = 0f;
	public float maxCorrectForce = 0f;
	public float minCorrectForce = 0f;
	public float maxDiffFract = 0f;
	public float minDiffFract = 0f;
	public float maxDot = 0f;
	public float minDot = 0f;

	void Update () {


		mass = rigidbody.mass;
		massAcceleration = mass*acceleration;
		inputDirection.Set (Input.GetAxis("Horizontal"),0, Input.GetAxis("Vertical"));
		inputDirection = inputDirection.normalized;
		inputDirection *= massAcceleration;
		if (inputDirection.magnitude < minWantAdd)
			minWantAdd = inputDirection.magnitude;
		if (inputDirection.magnitude > maxWantAdd)
			maxWantAdd = inputDirection.magnitude;

		velocity = rigidbody.velocity;
		velocity.y = 0f;
		currentSpeed = velocity.magnitude;
		if(velocity.magnitude < minVelocity)
			minVelocity = velocity.magnitude;
		if(velocity.magnitude > maxVelocity)
			maxVelocity = velocity.magnitude;

		speedDiffFract = 1.0f-(velocity.magnitude/maxSpeed);
		if(speedDiffFract < minDiffFract)
			minDiffFract = speedDiffFract;
		if(speedDiffFract > maxDiffFract)
			maxDiffFract = speedDiffFract;

		forceDot = Vector3.Dot (velocity.normalized,inputDirection.normalized);
		if(forceDot < minDot)
			minDot = forceDot;
		if(forceDot > maxDot)
			maxDot = forceDot;

		modifiedForce = inputDirection*speedDiffFract;
		if(modifiedForce.magnitude < minModForce)
			minModForce = modifiedForce.magnitude;
		if(modifiedForce.magnitude > maxModForce)
			maxModForce = modifiedForce.magnitude;

		correctedForce = Vector3.Lerp(inputDirection,modifiedForce,forceDot);
		if(correctedForce.magnitude < minCorrectForce)
			minCorrectForce = correctedForce.magnitude;
		if(correctedForce.magnitude > maxCorrectForce)
			maxCorrectForce = correctedForce.magnitude;

		rigidbody.AddRelativeForce(correctedForce);
}

Have done some more testing. Tried to clamp speedDiffFract to 0-1 but that was not good now Tried get even higher speeds. Think I’m approaching this in the wrong.

What i want to do is to get a smooth way to add force to a rigid body and when we get up to top speed i just add a little force so i hold that speed. Think of it like a way to find the right throttle when you drive a car. Instead of max throttle and when I’m at max no throttle. And then max again when the velocity drops.

this is a tutorial for that

check out the rest of his tutorials. they are awosme.

hope this helps. Im not a coder, just starting to learn.

sorry but its in java script, but it will show you how to solve the problem.

I have found them already and ETeeskiTutorials does great tutorials. And it works quite nice. But ill try to find a way to add force that dampens when I’m almost at max velocity so it’s not adding full force and that way balance the added force. With this control ETeeskiTutorials does. It’s like to add full throttle up to max after that set the velocity to the max. And when you drop your add maxfoce again. Resulting in a slight speed increase and drop.

Btw thanks for the input.

I’m a coder so I should solve this but not sure if I got the math and logic right. Was some time ago since I coded and im use to c++ but was not to hard to switch to c#