Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ronald.meijers · Jul 30, 2012 at 07:02 AM · rotationrotateeuleranglesmovetowardslocaleulerangles

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kryptos · Jul 30, 2012 at 08:03 AM 0
Share

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.

avatar image ronald.meijers · Jul 30, 2012 at 08:39 AM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ronald.meijers · Jul 31, 2012 at 06:12 AM 0
Share

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 :)

avatar image Owen-Reynolds · Jul 31, 2012 at 03:12 PM 0
Share

Thanks for bearing with my new-happy bug-ridden reply -- fixing my "answer."

avatar image
0

Answer by ScroodgeM · Jul 30, 2012 at 08:58 AM

if (Mathf.DeltaAngle(180, transform.localEulerAngles.z) < 1f)
{
    print ("Rotation completed");
}
 
              
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Transform.Rotate producing unexpected results when being used after setting localEulerAngles 0 Answers

Camera rotation behaving strangely 2 Answers

Bike stay upright based on surface normal 1 Answer

Rotate and eulerAngles don't work together ?? 2 Answers

Smoothing eulerAngles rotation? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges