- Home /
2d detect 360 (backflip) rotation on z axis?
Hi,
So I am making a 2d car game and am trying to implement scoring points based on the amount of backflips completed. I am not sure how to calculate (once the car has left the ground) if it has completed a full 360 degree turn anti-clockwise to then score a point. Does anyone know how I detect this?
Any help is greatly appreciated, thanks.
Answer by Teravisor · Jan 26, 2016 at 08:40 PM
It's so simple... I don't know how you didn't thought of that...
You can start counting it only when you change state from on-ground to airborne.
Each frame add to some variable that counts how much you turned while airborne.
Until you're out of airborne state. Then check which angle you've changed since 1. and score a point if you did do >=360 degree turn. Stop counting.
In order to check exactly counterclockwise and not just any angle, you need a simple math: angle between Vector(1,0) and Vector(x,y) is Mathf.Atan2(y,x);
(returns from -180 to 180 degrees in radians so multiply by Mathf.Rad2Deg and check when it goes from -179 to +179 or just use RigidBody2D.inertia) then subtract previous value of same angle to find angle change. Positive will be counterclockwise and negative will be clockwise.
How do I count how much I have turned? Could you give an example of it?
Thanks for the answer though, it answers most of my queries, thanks @Teravisor
One potential approach would be to save your previous rotation on a frame, then compare it with your new rotation at the end of the current frame. For that, you could use something like:
Quaternion lastRotation = transform.rotation;
// Apply rotational changes
Quaternion rotationOnFrame = Quaternion.Angle(lastRotation, transform.rotation);
Or, in the case of using the physics engine, the process can be inverted, where the last frame's rotation is grabbed at the end of the script and the difference is compared before changing its value again.
Sorry for being a pain, but I am still confused. So do i set the lastRotation
when I leave the ground and rotationOnFrame
when I land again? Also, Quaternion.Angle is a float, not a Quaternion. How do I know the car has gone all the way round, and what if it goes more than 360 degrees? Thanks for the help @$$anonymous$$o $$anonymous$$haon
Your answer
Follow this Question
Related Questions
2D rotation and moving forward 2 Answers
Localscale-flipped 2D character retains original rotation since 5.4 2 Answers
2D rotation question 0 Answers
Rotate/Flip Image in Unity 2d using Virtual Joystick 1 Answer
Flip the player with arm rotation 2 Answers