Inok
1
So for assign rotation to gameobject with euler angles as input values i can do 2 ways:
-
transform.localRotation = Quaternion.Euler(x, y, z);
-
transform.localEulerAngles = new Vector3(x, y, z);
What way better and why (at performance point of view)?
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.
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.