Bending physics - turning rigidbody.velocity 90 degrees

Hi Guys and Girls.

I’ve been playing around with unity for a short while now and I love it! So I decided to do a simple race game to speed up my learning. So far I have been dissecting the racetrack demo from unity, and it works real well. But I wanted to do a little something extra. So I made a “batman harpoon style” 90 degree turn animation, where I animate the car drifting and turning hard 90 degree right or left, depending on user input.

Once the animation is done, my car has a new heading that is off by ± 90 degrees, but apparently my rigidbody is still heading in the old direction. And so at higher speeds, the car jumps all over the place.
I have tried using several code snippets I found on the forums. And what I have found works the best, goes somewhat like this:

    OldSpeed = rigidbody.velocity;
    float sin = Mathf.Sin(grader);
    float cos = Mathf.Cos(grader);

    float tx = OldSpeed.x;
    float tz = OldSpeed.z;
    OldSpeed.x = (cos * tx) + (sin * tz);
    OldSpeed.z = (cos * tz) - (sin * tx);

    rigidbody.isKinematic = true;
    // Do animation
    // Wait for animation to finish (non blocking ofc.)

    rigidbody.isKinematic = false;
    rigidbody.velocity = OldSpeed;

But this only works to some extent. At higher speeds, the problem still persists. And I honestly suck at mathematics, so figuring this one out by myself is just not going to happen.
So I’m hoping that there is a way I can rotate my rigidbody velocity 90 degrees around the Global Y axis. So that my car will keep going straight after the animation?

This will rotate the velocity 90 degrees:

rigidbody.velocity = Quaternion.AngleAxis(90.0, Vector3.up) * rigidbody.velocity;

That looks about correct, assuming grader is computed correctly. Even if you know the cos/sin math, easy to mess-up conversions, signs, forget to normalize… . I’m guessing some not-shown part isn’t quite right. But, here’s rotation using Unity’s tools (which are really just game tools):

Vector3 oldDir = rigidbody.velocity;
// freeze and turn over many frames
rigidbody.velocity = Quaternion.Euler(0,90,0)*oldDir;

Rotation (90) is in clockwise degrees. This wastefully saves and rotates the y speed, but it’s easier to read and, and not run often enough to care.

Might also try rotating the angularVelocity – otherwise if you were tipping back before the turn, it would change into a roll. But no need if movemement is all “flat.”