thx
I am doing basically this same thing using Quaternion.LookRotation and Slerp. What I am running into as a problem is that my object is rotating correctly on the Y, however it ends up faceing the Z of my object toward my intended target/object. I want the X value to be what faces my target not the Z. This is driving me insane, all due to how Unity looks at X Y and Z. Not sure why they didnt have Z as up and down rather than Y to begin with it is just odd to me. Just wish there were an easy way to tell the Slerp to use X as it’s nose to hunt down my target lol
Unity thinks of +Z as “forward.” It is an arbitrary decision, and different engines will choose differently.
The simplest solution here is to introduce an intermediate Empty object between the original parent and the model you are trying to turn.
* TurretRotationEmptyObject
* TurretModelWithXBarrel```
Orient it such that the +X of your turret is pointed in the same direction as the +Z of the Empty. Now you can use the Unity functions like RotateTowards or Lookat or LookRotation on the Empty, and the child object will rotate along as you like.
Sadly I tried this as well, it didnt work. The empty object seemed to take on it’s own life. I moved my script to the empty hopeing it would rotate for me instead of the actual object thus allowing me to “fake” rotate the X to where I needed it.
Halley thank you you inspired me to give it another try. I had to destroy that original gameobject cause somehow it was corrupt and nothing would make it work properly. When I recreated the new empty object and a whole new script and started from there. Once I gave the gameobject a -90 for Y and left the Model Object at 0 this oriented me correctly and suddenly I was able to get it to move. Where previously with the broken empty it would turn only one time to a degree that basically had it pointing in the dead center of the screen and would never move again. Who would have thought it was a buggy object that cause the script to stop working.
Hi
I’ve been having a similar problems that I’m pulling my hair out with.
I have an object, say A, placed at the center which is intended to always point towards another orbiting object, say B. A needs to rotate along its Z-axis, with the X and Y axes being zero, furthermore, it is B’s X and Y changing coordinates that are used to compute A’s direction. I have used the following script:
Vector3 targetPosition = new Vector3(target.transform.position.x, target.transform.position.y, this.transform.position.z);
transform.LookAt(targetPosition);
transform.Rotate(0, 90, 0);
Now this does the job, however the problem I have is that while A is rotating along its Z-axis, Y rotation flips from 0 to 180 (or sometimes -180) causing A to flip upside down. This happens as soon as the X coordinate of B changes from being positive to negative and vice versa. I’ve tried isolating the z rotation through another related object using A’s localEulerAngles but the flip in the Y axis distorts the z rotation in one instance going from -90 straight to 90, skipping 180 degrees.
If anyone has any suggestions I’d be much obliged.
Thank you