Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Inok · Oct 01, 2015 at 09:13 AM · rotationscript.

What is more efficient for assign rotation with euler angles?

So for assign rotation to gameobject with euler angles as input values i can do 2 ways:

1) transform.localRotation = Quaternion.Euler(x, y, z);

2) transform.localEulerAngles = new Vector3(x, y, z);

What way better and why (at performance point of view)?

Comment
Add comment · Show 1
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 hexagonius · Oct 01, 2015 at 09:38 AM 0
Share

Debug the Time, run one of them 1 $$anonymous$$illion times, Debug the Time again. do the same thing for the other. compare them.

2 Replies

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

Answer by Suddoha · Oct 01, 2015 at 09:55 AM

They're both pretty identical from a performance point of view. I've ran a test on my notebook with 10.000 assignments in one frame on keydown. This is, in most cases, far away from the common usage unless you've got a huge project in which this happens alot, like thousands of times.

It yielded 24-26 milliseconds for the first one. The second one yielded 22 milliseconds frequently, in a few cases 23 milliseconds. Tests were repeated a few times just to be sure.

Regarding the amount of assigments, this isn't much of a difference, so to say they're pretty much equal performance-wise (in average for one call: 0,0025 milliseconds = 2.5 microseconds for the first, 2.2 microseconds for the second). This is really nothing to worry about, use the one that you like the most.

An assumption:

The difference could be that the first one does an extra calculation in the property:

  • calculates a quaternion from the euler angles first in order to get the rotation

  • assigns the quaternion to the rotation

  • euler angles are now recalculated in the transform.localRotation property from the new rotation because they might not be stored but calculated on the fly - but that's an implementation details that i don't know

The second one:

  • just assigns the euler angles

  • recalculates the quaternion in the localEulerAngles property

So if it's really implemented like that, you'd save one calculation of euler angles.

Comment
Add comment · Show 3 · 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 Inok · Oct 01, 2015 at 10:36 AM 0
Share

So variant 2) transform.localEulerAngles = new Vector3(x, y, z); little better.

avatar image Suddoha Inok · Oct 01, 2015 at 10:55 AM 0
Share

As for your question, it's nothing to worry about. The one takes (on my laptop) in average about 0.1 to 0.2 microsecond less than the other, that's 1/10000 to 1/5000 milliseconds.

(I corrected the time units, i accidently used nanosecodns ins$$anonymous$$d of microseconds.)

avatar image Bunny83 Inok · Oct 01, 2015 at 11:47 AM 0
Share

The test is most likely not representative. 24 milliseconds sounds way too much to me.

@Suddoha: What did you use to measure time? Did you restrict your thread to one CPU core? Do you have the transform property within your test or do you use a local variable?

When i run a similar test with 100000 iterations 100 times i get these results:

 localEulerAngles: avr: 18.998432ms ($$anonymous$$: 18.6519ms / max: 20.1713ms)
 localRotation: avr: 22.733399ms ($$anonymous$$: 22.2308ms / max: 23.3938ms)

localRotation is about 20% slower than localEulerAngles. However i guess it's due to the 1 additional float value being passed around (a quaternion consists of 4 floats) as well as this implementation of the Euler method:

 public static Quaternion Euler(float x, float y, float z)
 {
     return Quaternion.Internal_FromEulerRad(new Vector3(x, y, z) * 0.0174532924f);
 }

They first convert degrees to rad as their internal method seems to only work with radians.

edit
$$anonymous$$y benchmark was in the Unity editor. I created a build and i got those results:

 localEulerAngles: avr: 14.380257ms ($$anonymous$$: 13.9803ms / max: 15.132ms)
 localRotation: avr: 17.36501ms ($$anonymous$$: 16.9045ms / max: 18.2835ms)
avatar image
2

Answer by Bunny83 · Oct 01, 2015 at 09:48 AM

Both lines do the exact same thing: converting 3 euler angles floats into a quaternion. Efficiency is completely irrelevant here. Both only use value types (float, Vector3, Quaternion) so no garbage should be created by non of them. "localEulerAngles" is there to simplify getting / setting eulerangles.

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

33 People are following this question.

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

Related Questions

Why is gameobject rotating by 90 degrees only once? 1 Answer

turning a project using AddTorque 0 Answers

[C#] Rotate Turret, Only On X and Z Axis 2 Answers

Change mouse rotation point 1 Answer

Player Rotation Movement 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