I posted this earlier, then got so annoyed at the comments, I deleted it. I’m going to repost and try to be more concise… I’m going to get to the point first, then explain some things afterwards.
Suppose you have an object “O” and want to rotate it by a quaternion Q. You need to decide if you want it to rotate around the world axis, or the object’s own local axis. (Most of the time I want it to rotate around its local axis)
W: O.transform.rotation = Q * O.transform.rotation <- this rotates around world axis
L: O.transform.rotation = O.transform.rotation * Q <- this rotates around its local axis
That’s it. Those are the two forms you need to remember. The way I remember the order is because (as the docs say), when Unity runs across A mul B, the * operator override performs the A rotation first (LHS), then applies the B operation next (RHS). In my head I phrase it “gives it an extra spin at the end of everything”. Iif you give it the extra spin before its normal spin, that’s not going to happen “locally”. That’s a global (world) spin.
Another sort of non-obvious result of this is that if you have a cube at a distance from 0,0,0, and you want to rotate it in a giant circle around say the Y axis, like a planet, you can’t do that by applying varying rotations on the transform, without that object having a parent, and even then you have to rotate the parent, not the object itself. If you were to try to rotate the object “around the world axis Y”, it would simply spin in place around the up and down axis in the world. Not spin around 0,0,0’s Y axis. Obvious to some, but not to everybody.
Somebody wrote me a private message and said, “you don’t know what you’re talking about wrt local rotations and parents. Nothing in Unity when it comes to setting rotations and positions matters with regards to if it has a parent or not”. So polite. Well, it’s true and it ain’t true. Let me explain…
Put two identical objects into a scene. One of them has a parent, one of them does not. The one with a parent, the parent has some non-zero position and rotation offset. I set both the objects’ world position and rotation to something interesting. I can do it via independently setting position and then rotation, or I could call SetPositionAndRotation. Both the objects end up in exactly the same world position and world rotation. So what’s the difference? The difference is that under the hood, during the = assignment for both position and rotation, Unity peeks at where you want it to be, and calculates where it already is (by looking at the parent chain), and it calculates deltas, and sets the local component to whatever it needs to be in order to get the object to where it should be. This applies to position AND it applies to rotation. In fact, rotation is “a bit special”. When you assign a world rotation to an object that has a parent, it will change both the local position and the local rotation to get the object to the exact spot you’ve told it to be at. I find this interesting…