Question by
TashaSkyUp · Mar 15, 2017 at 07:59 AM ·
c#rotationz-axistower
Torrent rotation to face an object rotating on Z axis only. Define facing axes as X
There are many totorials out there on how to rotate in variety of methods to face another object. They all take for granted that the user has set up their scene so that rotating around the Y axis which i guess is default, works.
Ive already set my scene up so that going deeper into the screen increases the Z coordinate. left/right is X up/down is y much more normal if you ask me.
What I need to do is rotate the torrent object around its own Z axis so that its X axis is facing a target object/transform.
Thank you, please do not point me to other posts or tutorials that do not specify rotation around the Z axis.
Comment
Best Answer
Answer by TashaSkyUp · Mar 15, 2017 at 11:28 AM
Vector3 targetDir = target.transform.position - transform.position;
targetDir = targetDir.normalized;
targetDir.z = 0;
var a = Vector3.Angle( -transform.up, targetDir); //note does not return negative angles
//to find angles that should be negative and make them that way
{
var cross = Vector3.Cross(-transform.up, targetDir);
if (cross.z < 0) a = -a;
if (a > 0) {
a = Mathf.Min(turnRatedeg, a);
}
if (a < 0) {
a = Mathf.Max(-turnRatedeg, a);
}
}
transform.Rotate(new Vector3(0, 0, 1), a* Time.deltaTime); //rotate on the z axis