# Rotating to face a world position only on the Z axis

I have a Transform with a mesh and a material to represent an in-game direction indicator.

This is parented to another Transform called Pivot.

I am trying to rotate the Pivot transform on its own Z-AXIS only so that it points in the direction of the target.

https://i.sstatic.net/KzucI.jpg

I feel like i'm missing a variable here but I cannot figure out what, I have tried many versions of code found online and I could not get any of them to work for my specific case, some didnt work at all, some got close but we're always off, some seemed to do the opposite of what I wanted but inverting that result got me an entirely different result.

I have read many answers on here about this topic already, some I'm positive answer my question but I cannot take that information and apply it to my own problem, I am just not very good with this kind of problem, so I apologize if you are reading the same question again.

Thanks.

i'm not sure what good code from failed attempts will do, but I have tried setting the Pivots.right to the direction to the target.

  Pivot.transform.right = offset;


The below code also got me close but didnt seem to follow the sphere correclty sometimes it would at that the target, other times slightly to the side, never really correct.

    Quaternion rotation = Quaternion.LookRotation(
Vector3.up + Vector3.forward,
offset
);

// Apply a compensating rotation that twists x+ to y+ before the rotation above.
Pivot.transform.rotation = rotation * Quaternion.Euler(90f, 0, 0);

Pivot.transform.eulerAngles = new Vector3(0f, 0, Pivot.eulerAngles.z);


This gets me pretty close but gets weird when it comes to doing a full rotation when passing below zero.

        Vector3 offset = target.position - Pivot.transform.position;
Pivot.right = offset;
Pivot.localEulerAngles = new Vector3(0,0, Pivot.localEulerAngles.z);

• To be clear, you want to rotate the object about its Z+ axis, so that its X+ axis points toward the target? (In your question you just say "it" so it's not 100% clear which axis should do the pointing) Feb 20, 2022 at 0:29

Quaternion.LookRotation(forward, up) creates a quaternion that will rotate the z+ axis to point exactly along the forward argument, and the y+ axis as close as possible to the up argument.

So we can apply this in two steps. First, make a quaternion that rotates the x+ axis to the y+ axis, then a quaternion that makes the y+ axis point toward the target, and chain the two together:

var xToY = Quaternion.LookRotation(Vector3.forward, Vector3.left);
// Quaternion.Euler(0, 0, 90) would also do this job. Note this is a constant either way.

var yToTarget = Quaternion.LookRotation(transform.forward, targetPos - transform.position);

transform.rotation = yToTarget * xToY;


This is also equivalent to:

// Make a vector perpendicular to the direction we want our x+ to point.
var perpendicular = Vector3.Cross(transform.forward, targetPos - transform.position);

// Point our y+ axis that way, so our x+ axis points the way we want.
transform.rotation = Quaternion.LookRotation(transform.forward, perpendicular);


Of course, you could save some steps if you just orient your arrow graphic so that it points along the y+ axis to begin with. 😉

• thank you very much this is very close, it seems to only point correctly towards the item im tracking when I face that item from a particular direction, I can't figure that out either, I will update my answer with the code im using and a few screenshots to illustrate the issue, thank you again. Feb 20, 2022 at 1:04
• Sorry to add on, this is a mod for a game that's made in Unity so some things I dont have much control over but it would be much easier! Feb 20, 2022 at 1:09
• Would rotating the literal arrow transform until it matches the Pivots ..local(?) X axis do what I want too? Sorry this confuses me a lot! Feb 20, 2022 at 1:14
• You changed the code I gave you. I did not say to subtract the pivot's transform position from an offset (that makes no geometric sense) but to subtract it from the target position. Feb 20, 2022 at 1:19
• Sorry about that, you're correct of course never doubted the problem was with me and not your code! it's working perfectly - I am so out of my depth with this kind of problem, thank you again for taking your time to answer something I know comes up often. I wish I understood why that makes no geometric sense! Feb 20, 2022 at 1:25