- Home /
Detecting 360 degree spin on local y axis
Maybe it's a simple questioan and there are some similar topics here, but nothing really helped me and I am stucked with this problem for 3 days now.
I have got an physics driven object and i want to track its "local y" or transform.up rotation on an triggered event. I want to know if it did a 360 degree spin. A good example for this problem is when the player makes a frontflip and you also want to know if he did some spins within.
Thx
Answer by Mark Gossage · Jun 09, 2015 at 01:52 PM
((Expecting an answer in 35 minutes is a bit much!))
To detect ANY rotation, you can just compare transform.localRotation
to the previous rotation using Quaternion.Angle()
If you wanted for just one axis, you could use transform.localRotation.eularAngle
and compare to previous angle. Use a Mathf.DeltaAngle
to overcome the 0/360 issue.
((Expecting an answer in 35 $$anonymous$$utes is a bit much!))
It was asked one $$anonymous$$onth and 35$$anonymous$$s ago ;)
Quarternion.angle was my first intention too, but here I have the problem if i do just a horizontal spin it will work, but if I do a spin combined with a front or backflip it tracks just the delta on an different axis.
Answer by Immanuel-Scholz · Jun 09, 2015 at 09:39 AM
So, if I understand you correctly, you have an triggered event from outside, when the tracking begins (e.g. the biker jumps in the air). From then, you want to track and get informed when the bike spinned 360° in any direction, right?
Then here is something out of the blue (means: I didn't even try to compile it ;)) that might get you an idea how I would approach this.
class DegreeTracker
{
float angleSoFar, angleLastFrame;
void Start() { enabled = false; } // no tracking by default
// called from somewhere when the tracking begins
public void StartJump()
{
angleSoFar = 0;
angleLastFrame = transform.localEulerAngles[1];
enabled = true;
}
void Update()
{
float angle = transform.localEulerAngles[1];
angleSoFar += angleLastFrame - angle;
angleLastFrame = angle;
if (angleSoFar > 360)
{
// code for 360 clockwise comes here
enabled = false; // finished circle, so disable tracking
}
else if (angleSoFar < 360)
{
// code for 360 counter clockwise comes here
enabled = false; // finished circle, so disable tracking
}
}
}
Basically, you remember the angle from last frame and sum up the difference each frame. If you hit > or < 360, you got your circle.
Simply subtracting the values from eulerAngles parameters will not work. You would expect (1 degrees) $$anonymous$$us (-1 degrees) to be (2 degrees), while the plain subtraction gives you (-358 degrees).
Hope this helps!
I have some problems with the local and global eulerAngles. Here I got the same values...
I don't know if this helps: The player object has some childs, but the tracked object is the parrent. when I try to track one of the child objects, there is a difference on localEuler, but until they a re not rotating relative to it's parent they don't give any deltas. $$anonymous$$aybe I have prolems to understand the difference between localEuler and global ones.
Is there an opposite function like Quarternion.AngleAxis but wich gives me the degrees as return value on an fixed Axe?
Local rotation is simply the rotation relative to the parent of the object. This is what you see in the "Transform" component when you open the object in the Unity "Inspector" window.
The global rotation is.. well.. the rotation in the world. So that is what you see when you see the object in the scene view. If its tilted, then its global rotation is tilted.
So if you want to track a global rotation (means: You don't care which parents of the tracked object gets rotated, as long as you know when the overall object finally spun one circle), then you need to track "eulerAngles" ins$$anonymous$$d of "localEulerAngles".
"eulerAngles" is also the desired "opposite function" you where looking at (as long as the x- and z-rotation is 0).
And fzawd is right - you might experiment with $$anonymous$$athf.DeltaAngle() ins$$anonymous$$d of substracting values to get around the 360->0 border problem..
Your answer
![](https://koobas.hobune.stream/wayback/20220613195158im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Get raycast to ignore rotational axis 2 Answers
Flip over an object (smooth transition) 3 Answers
Camera Rotation stay behind player and not flip 2 Answers
Sprite disapears when moving left 2 Answers
rotate vector around local axis 2 Answers