Keeping forward velocity always in the same direction as transform.forward

Hi, not sure if my title is clear enough.
I’m making a racing game inspired by the airplane parts in Diddy Kong Racing and the pod racing in Star Wars Episode 1 Racer.

My code worked pretty well with using translate to move the plane, but since i needed collisions i changed that to rigidbody.addForce. (the -1 is needed because i… uh messed up the direction when modeling my test vehicle :P)

rigidbody.AddForce (this.transform.forward * (-1) * acceleration, ForceMode.Acceleration);

Now i have a problem since whenever i turn the plane it keeps sliding forward until the velocity from the original direction disappears, also it takes a while until there is enough velocity into the new direction. I tried working around with drag, but while it makes away with the sliding, it makes the acceleration at the start and when turning unbearable.
My Horizontal turning code:

transform.Rotate (new Vector3 (0, horizontalRotateStrength * horizontalRotationDirection * Time.deltaTime, 0), Space.World);

I quickly got a solution for that from a friend

rigidbody.velocity = Quaternion.Euler(new Vector3 (0, horizontalRotateStrength * horizontalRotation * Time.deltaTime, 0)) * rigidbody.velocity;

it actually works well enough, but it’s not applicable to the vertical rotation which works like this:

transform.localEulerAngles = new Vector3 (Mathf.LerpAngle (transform.localEulerAngles.x, invertY * verticalRotationDirection * maxVerticalRotation, Time.deltaTime * verticalRotateStrength), transform.localEulerAngles.y, transform.localEulerAngles.z);

Any ideas how to solve the problem? It feels like a problem that should be simple to solve, but somehow i’m drawing a blank

P.S. I also use addForce to simulate gravity among other things. I imagine that could make things more difficult, maybe.

The easiest way to restrict the veloocity in “local space” is to transform it to local space, apply your restrictions and convert it back to world space:

edit

    // C# / UnityScript
    var yVel = rigidbody.velocity.y;
    var localV = transform.InverseTransformDirection( rigidbody.velocity );
    localV.x = 0.0f;
    localV.y = 0.0f;
    var worldV = transform.TransformDirection( localV );
    worldV.y = yVel;
    rigidbody.velocity = worldV;

This would eleminate all sideway speed and only preserve forward / backward speed and the world space up / down speed like gravity.

How about just upping the Drag setting in the Rigidbody component. You may have to up the force you apply at each frame to compensate. But it would cause the old forward force to decay faster reducing slide. You could also dynamically change the drag and/or the velocity vector based on the rate of turn. So the plane glides with flying straight, but catches more resistance based on the steepness of the turn.

Or you can cancel it completely by using the dot product of the forward and velocity vectors and only preserving the part of the velocity that is going in the new direction.