localRotation will ignore any parent by definition, yet you rely on transform.position meaning that you’re operating in the world space, but then you modify the object space transformation.
You must be mindful of the spaces when you’re working with linear algebra in this. We use localRotation because it is de facto the storage of the actual quaternion being applied locally. It is faster and more precise.
But when you’re operating in the world space – and when you have a turret aiming at some free target out there, that’s a clear case of world space operation – we rely on rotations and positions instead of their local counterparts.
The difference is that rotation and positions have to take the hierarchy into account to be truly representative of the world space. Getting/setting them is slower and less precise, especially with scale (which is why it’s called lossyScale), the reason being that the matrices are composited super-fast, but then you need to decompose the final matrix, to get the separate results, such as a quaternion, from it.
This is not super slow in the general case like yours, and you definitely want to work like this, but here I’m trying to explain why we use localRotation and localPosition historically. And Unity actually uses these to KEEP the original information without having to pack/unpack matrices all the time. However as soon as you start building up a hierarchy, the actual result you see on the screen is all converted into world space matrices, and the original information is KEPT but not really rendered (unless you change the values, then it has to propagate upward to compute the world space again). If you attempt to change the object-space information to values that are derived from the world-space it will probably be buggy and non-sensical, unless you had a super-simple identity hierarchy to begin with (i.e. parent’s rotation is 0,0,0).
With all that in mind, you can however, convert the result from the world space to object space on the fly, if you really need to, by multiplying Transform.worldToLocalMatrix
(it gets pretty advanced from there, and I don’t recommend this if you’re not familiar with matrices).
(Edit: Ok, you’re using eulerAngles to avoid having rotations on Y and Z. For some reason I read Quaternion.Euler, please disregard the next paragraph for this case, but it’s a useful advice anyway. There are btw ways to avoid eulerAngles just as well, you don’t need that.)
Btw you don’t really need Euler angles for what you’re trying to achieve. Euler angles are not really useful unless you’re the one driving them – i.e. by actually setting the angles to some “designed” rotation. I have never seen a live algorithm employing a naturally occurring reorientation (i.e. turret aiming) that needs Euler angles. Euler angles transformation is super-slow and unreliable and should be used only as an in-code human-translation tool for when you really want to make a quaternion but don’t want to mess with the weird values and you want your code readable. You call it once however and you’re done with it, that’s how one uses it.