- Home /
localEulerAngles don't seem to work in an equasion
Hi,
I'm having a weird issue. The idea is simple: rotate an object with Mathf.MoveTowards, and when the rotation is completed, do something.
Here's what I wrote:
void FixedUpdate () {
//Set Speed
int speed = 50;
//Rotate object
transform.localEulerAngles =
new Vector3(0,180,Mathf.MoveTowards(transform.localEulerAngles.z, 180, speed * Time.deltaTime));
//When the rotation is completed, print it
if(transform.localEulerAngles.z == 180){
print ("Rotation completed");
}
}
Everything works fine up until the last 'if' statement. That never seems to get triggered, but when I print the values to the console, they ARE equal at the end!
What bothers me ever more, is that when I use this same procedure, but with localPosition, it DOES work!
So I'm getting the feeling the localEulerAngles are bugged or broken in some way.
Any thoughts?
Changing the values of localEulerAngles or eulerAngles at runtime is not a very good idea (especially when you have one or more values that are close or equal to a multiple of 90 degrees). You should consider using Quaternion ins$$anonymous$$d.
Alright, I did find a sloppy workaround using $$anonymous$$athf.Round, to round the angles in the equation to an integer, which seem to 'solve' it for now.
Answer by Owen-Reynolds · Jul 30, 2012 at 03:37 PM
Any given eulerAngle is unstable -- like someone saying the time is 20 past noon, then a minute later saying it's 39 minutes to one, then next minute going back to 22 past noon. Looking at just euler z is like looking at the minutes "randomly" snapping from 20 to 39 to 22. The docs mention this, sort of -- they say not to read/set just one (you're reading only z.)
Euler angles tend to only snap around 90, 180, 360. So, you figure can work around that. But the more ways you move, the more you'll get new snaps later.
The trick for setting with eulers is to "pull out" your angle variables, which keeps them consistant:
float zAng; // global
zAng=Mathf.MoveTowards(...);
// The official way to set a rotation from x,y,z rotations,
// instead of setting EulerAngles= :
transform.localRotation = Quaternion.Euler(0, 180, zAng);
// (^^ EDIT -- fixed based on comments ^^]
if(zAng==180) ...
The idea here is that zAng
, being your variable, will never do anything strange. Unity may still set take (0,180,90) and rewrite it as (-90,90,0) -- same facing but diff numbers. Before, that would have made you start over at 0. Now it won't.
Thanks for the explanation!
But I'm getting: The nested type Euler' does not exist in the type
UnityEngine.Quaternion'.
Edit: Removed the 'new', now it rotates, but at ridiculous speeds, trying to fix that
Edit2: Alright, I needed to use transform.localRotation ofcourse :)
Thanks for bearing with my new-happy bug-ridden reply -- fixing my "answer."
Answer by ScroodgeM · Jul 30, 2012 at 08:58 AM
if (Mathf.DeltaAngle(180, transform.localEulerAngles.z) < 1f) { print ("Rotation completed"); }
Answer by ronald.meijers · Jul 30, 2012 at 09:35 AM
Thanks, that seems to be somewhat the same as what I got now:
if(Mathf.RoundToInt(transform.localEulerAngles.z)==180)
{
print ("Rotation completed");
}
Although that means it loses it's accuracy with tenths of degrees. Luckily I can get away with that in this project, but for future purposes I might want a perfect acurate solution. Too bad I suck with Quaternions :P