- Home /
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?
Answer by robertbu · Dec 09, 2013 at 04:44 PM
This will rotate the velocity 90 degrees:
rigidbody.velocity = Quaternion.AngleAxis(90.0, Vector3.up) * rigidbody.velocity;
Problem solved!
Such a "simple" line of code, and to think of all the time I used looking trough much more advanced examples. And this is all it took. :)
I wish there was a formulary, along the lines of a physics or mathematics book. Where things was listed according to use, area of subject or something like that.
The online documentations that comes with unity is great - if you know what to look for. But being a newb at this, I've used countless hours looking trough the documentation, reading the forum and answer section.
Of course I've learned a lot along the way, but I have wasted a lot of time doing so.
Anyways, thanks for helping both of you.
Answer by Owen-Reynolds · Dec 09, 2013 at 04:47 PM
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."
Sorry about that, grader is of course degrees to turn. Its Danish btw. :)
Thanks for a great answer. As you write, your solution works as long as there is no roll. But as I discovered, the car apparently rolles sometimes, going into a turn. And then strange things happen. I will try the angularVelocity, and see what results I get.
And that's your problem ;) in almost all program$$anonymous$$g languages the trigonometric functions work in radians and not degrees.
http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$athf.Sin.html
The example on the page is, again, a very bad example. They should have used $$anonymous$$athf.PI/2 (which would be 90°) ins$$anonymous$$d of 3 which would be some strange angle in degree
Your answer
Follow this Question
Related Questions
Rotating a moving object 0 Answers
Rigidbody Disable Velocity/Movement? 1 Answer
A way to kill rotation made by rigid body pysics? 1 Answer
Adjusting Roll and Pitch with Velocity 0 Answers