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?