This is just a quick take on what Unity users seem to believe they need to understand inside and out: Quaternions
The Unity documentation for the Quaternion class is all you need to reference and use as a black box to the magic numbers of Quaternions.
If you understand Euler Angles, you can get along well enough to work with Quaternions. You don’t need to understand the inner workings of Quaternions. Unless you are really interested in them, spend that extra time making a fun game.
I used the Quaternion class with Unity’s Physics for Absolute Territory and A-Spec First Assault. It was more difficult for using use rotational forces rather than the built-in rotation methods made available.
As a side note: You use the Rigidbody Inertia Tensor for mass with rotations, and Mass for translation if you want to apply forces to simulate a Reaction Control System.
I recently started looking at performance optimization applicable in Unity. Doing Google searches showed out-of-date tests (much like this could be if you are reading in the far future).
I’m specifically trying to determine which type of loop (For Vs. ForEach) is the most efficient to use as 1) referencing GameObjects and 2) modifying a value.
For these tests I’m using Unity 2020.3.19f1 LTS on Windows 10 with an i9-10850 CPU with 16GB RAM. For updating values, the GameObject position was changed. I ran the test seven times and used the average to come to a conclusion. This is more as a reference rather than the “be all and end all” of which case suits your best. Basically, do your own tests to see how each case would suit your own needs.
Without further ado, here is are my test results iterating an array of 100,000 GameObjects:
Results
Editor
IL2CPP Editor
Read Value
Set Value
For
ForEach
For
ForEach
0.003391743
0.003664017
0.06759405
0.04567719
0.003417492
0.003528595
0.06367445
0.04667139
0.00346756
0.003520012
0.06682873
0.05114985
0.003381252
0.00334549
0.06244278
0.04349947
0.003490448
0.003461838
0.06083345
0.04655266
0.003408432
0.003543377
0.06335735
0.04396629
0.003567219
0.003522873
0.06211138
0.04444933
Editor For v ForEach Times in Seconds
Average
0.003446306571
0.003512314571
0.06383459857
0.04599516857
Difference
0.000066008
-0.000066008
-0.01783943
0.01783943
Editor For v ForEach Time differences in Seconds
Player Build
IL2CPP Player
Read Value
Set Value
For
ForEach
For
ForEach
0.0006356239
0.0005478859
0.04321384
0.0290904
0.0006337166
0.0005435944
0.04184103
0.02432728
0.0006260872
0.0006694794
0.0397625
0.02416563
0.0006256104
0.0005288124
0.03798485
0.02457619
0.0006494522
0.0005488396
0.04362774
0.02631426
0.0006275177
0.0005335808
0.03851748
0.02418947
0.0006356239
0.0005555153
0.03823924
0.02412081
Player For v ForEach Times in Seconds
Average
0.0006333759857
0.0005611011143
0.04045524
0.02525486286
Difference
-0.00007227487143
0.00007227487143
-0.01520037714
0.01520037714
Player For v ForEach Time differences in Seconds
Conclusions
First off, Unity Editor has a lot of overhead and is going to be always much slower than a build. Testing in Unity Editor will help determine if there are easy performance gains. When you want to get more realistic values you need the Player build used by players.
Comparing Timings between the Editor and Player build when reading elements in an array
Comparing the Editor to the Player build, and reading values from an array timings move from the 100ths into the 1000ths of a second. Setting values is much slower than reading values measured in 10ths. Modifying elements in a list has a performance cost, so don’t update elements needlessly (much like you shouldn’t in any kind of game loop, i.e. Unity’s Update()).
Reading elements in an array
Does it significantly matter if you are using a For or ForEach loop when reading elements in an array? We are talking 10,000ths of a second. Not a significant difference between the two. If you want the fastest iteration possible then ForEach wins when reading elements in an array.
Comparing Timings between the Editor and Player build when writing elements in an array
Modifying a GameObject value has its performance cost (probably more due to using the position Vector3 array which is a struct) and the difference is 2/100ths of a second, and significantly more costly than just reading values.
Writing elements in an array
ForEach is 15/100th faster than For. This difference is not going enough to worry about on its own but ForEach is faster and the best choice for getting as much performance as you can.
Other considerations
A last-minute thought, quite literally, any garbage collection from using a For or ForEach. Several years back ForEach generated garbage, then an update improved its performance. This experiment has not taken this into consideration and could make a considerable difference in how much Garbage is generated and thus make Garbage collection run more frequently, slowing performance and increasing frame hitches.
Testing on a variety of target machines could impact performance differently.
Using Mono scripting backend could impact performance differently.
TL;DR: ForEach was faster than For in this experiment. That alone should not influence any decision-makingprocess.
UPDATE
Garbage collection was skewing the results. The first time you access the .transform for a GameObject Unity generates garbage.
I got around this by just doing an initial iteration before testing the times. From some initial testing ‘For’ won 4 out of 7 times in the 1/100th’s of seconds.
Based on this there is not much potential for performance gains. It’s best to choose for or foreach based on the needs of the project.
This garbage generation on first time accessing the transform may be worth noting for anyone who pools gameobjects.