- Home /
Keeping track of vehicle rotation?
Hey guys, I'm trying to figure out how to tell when my vehicle does a complete 360 degrees flip. What I need to do is when the player jumps the bike, is store the current rotation, and as the bike rotates backwards or forward, I need to keep track of the rotation and detect when it gets back to the starting rotation stored when the player first started the jump.
I am not quite sure what to use to store the starting rotation. Should I use rigidbody.rotation or trabsform.eulerAngles or maybe transform.up...then I imagine keeping track of the rotation would be something similar to:
// Starting rotation
Vector3 startRotation = Vector3.Zero;
if( bikeJustLeftGround == true )
{
startRotation = transform.eulerAngles;
bikeJustLeftGround = false;
}
// rotation of bike compared to starting rotation
float currentRotation = Vector3.Dot( transform.up, startRotation );
// And then...
if( currentRotation == startRotation.z )
{
Debug.Log("DID A FLIP!!!");
}
I can't really get the above code to behave the way I need it to behave. Rotations confuse me and I need some help on this one for sure haha
Thanks for your time guys! Stephane
Answer by robertbu · Jan 11, 2013 at 07:47 AM
Quaternion.Angle() is one good bet for this problem. Instead of saving the eularAngles do something like:
Quaternion qInitial = transform.rotation;
Then you can test for the current rotation with:
Quaternion.Angle(qInitial, transform.rotation);
I don't know how your game works, but you might first have to detect that the vehicle made it past 90 degress first and then give some sort fudge factor (i.e. the flip is successful when the angle is back under 20 degrees).
Hey robertbu, thanks for the example. I ended up using a state machine and detecting when the Dot vector is between a & b and switching states, but I'm gonna try with Quaternion.Angle() as you suggested, might be easier to read and also less code.
Your answer
Follow this Question
Related Questions
Using Quaternion.LookRotation( ) on y axis only, but keep x axis of transform the same? 2 Answers
Writing a custom LookAt() function 2 Answers
Alternative to Quaternion.LookRotation() ? 2 Answers
How do you smoothly transition/Lerp into a new rotation? 3 Answers
Edited mouselook script rotation clamp not working 0 Answers