Calculating speed and constant speed in case of AddRelativeForce ()

Hi everyone,

I have faced a problem related to RigidBody.AddRelativeForce(). I have read tons of answers concerning how to deal with Rigid Body.AddRelativeForce() but still can’t figure out my problem.

My target is to move the spaceship in space and most important is to control the velocity of forward/reverse movement and both vertical and horizontal strafe. Velocity of XYZ movement will be displayed via GUI elements. What’s more, I set certain maximum velocity limits which are different for X, Y and Z.

Well, speaking about my code, I decided to control velocity for X, Y and Z separately, but code is similar except parameters of max velocity limit:

if(rigObject.transform.InverseTransformDirection(rigObject.velocity).z < shipVelocities.MaxForwardVelocity){
accelearationForce += (power * Time.fixedDeltaTime * shipVelocities.AccelarationSpeed);
currentZAxisVelocity = accelearationForce;
}
if (rigObject.transform.InverseTransformDirection(rigObject.velocity).z >= shipVelocities.MaxForwardVelocity)
{
currentZAxisVelocity = currentZAxisVelocity;
if (accelearationForce != 0f) accelearationForce = 0f;
}

...

rigObject.AddRelativeForce(currentXAxisVelocity, currentYAxisVelocity, currentZAxisVelocity);

There is 2 problems:
currentZAxisVelocity is not equel to rigObject.transform.InverseTransformDirection(rigObject.velocity).z, because my rigidbody have Mass as 5 and Drag as 0.1. Is it possible to calculate real velocity knowing force applied to object, Mass and Drag?

And second question is that RigidBody is still accelerating, according my code, and I understand that it should be, but my purpose is constant speed and how doing it?

Thanks in advance.

I’m not sure but are you looking for something like that:

It makes not much sense to limit the velocity in a local direction. What when you rotate while you’re moving? Your approach would cause a slowdown. With the methods i’ve posted over there you can calculate what acceleration you need to read a certain velocity. So you can simply apply different forces when accelerating forward / backward / up / down / left / right.

Keep in mind that the acceleration is independent from the objects mass. If you want to apply a real force you have to multiply the required acceleration with the rigidbody’s mass. However, it’s usually easier to use ForceMode.Acceleration which ignores the mass.