# Getting just z orientation from this script

Hi all!

Just wanted to know if there is a way to get only the z orientation out from this script.

``````this.transform.rotation = Quaternion.LookRotation (this.positionToMoveTo - this.transform.position, Vector3.up);
``````

at the moment is taking every rotation axis in consideration. I just want the z axys! thankyou!

The two parameters that LookRotation takes are the Forward axis (Z-axis, blue axis), and Up axis (Y-axis, green axis).

So you are going to wind up with a rotation that has the green arrow pointing at the World Up vector, and a Z axis that points towards â€śpositionToMoveToâ€ť. The X axis must change in order for this to happen. If you just want one Euler Angle from the result, you can use â€śeulerAnglesâ€ť on the resulting Quaternion.

So â€śfloat z = Quaternion.LookRotation(Z, Y).eulerAngles.zâ€ť would give you the resulting Z angle from LookRotation.

so I have to delete all the script I posted and insert the one you gave me in that line?

What do you mean by â€śorientationâ€ť do you mean the angle that the Z axis is rotated, or do you mean the direction that the Z axis is pointing? Do you want the LookRotation to be applied, and then get the resulting Z angle or direction? Are you trying to find the direction pointing towards the â€śpositionToMoveToâ€ť?

1 Like

okay Iâ€™ll try to explain better. I need that the object rotate only on the y axis according to the rotation of the axis of the position to move to.

There are a few overrides that you can use in Transform.Rotate. Which allow an object to rotate based on another objects position, and the rotation amount you want.

``````this.transform.rotation = Quaternion.LookRotation (this.positionToMoveTo - this.transform.position, Vector3.up);
``````

Could be summarized as pointed above. But if you â€śjust want a Z axisâ€ť:

``````this.transform.rotation = Quaternion.LookRotation (this.positionToMoveTo - this.transform.position, Vector3.up);
var z = transform.rotation.eulerAngles.z;
``````

Not sure what you want to do with the code, so youâ€™ll need to figure out what you want to do with that data

If you want Z without rotating the object, then, donâ€™t rotate it, as mentioned above by
jeffreyschoch

``````var z = Quaternion.LookRotation(this.positionToMoveTo - this.transform.position, Vector3.up).eulerAngles.z;
``````
1 Like

Ok Iâ€™ll try to explain even better! I have a character who moves on a node based system. Basically on the tap action he will move to the node and take the position and the orientation of the node. And when I say orientation I mean x, y and z rotation values. But since the character should never rotate on x and y axis I need only the z rotation value of the node is moving to. I donâ€™t know if this can help you out to help me out!

Is this a 2D game?

If you have a reference to the Transform of the next node, then you can do this:

``````Transform nextNode;

transform.position = nextNode.position;

Vector3 newRotation = transform.rotation.eulerAngles;
newRotation.z = nextNode.rotation.eulerAngles.z;
transform.rotation = Quaternion.Euler(newRotation);
``````

itâ€™s a 3d game

``````hile (elapsedTime < timeToMove) {
transform.position = Vector3.Lerp(initialPosition, positionToMoveTo, (elapsedTime / timeToMove));
this.transform.rotation = Quaternion.LookRotation (this.positionToMoveTo - this.transform.position, Vector3.up);
elapsedTime += Time.deltaTime;
yield return null;
}
``````

I just want the z rotation values of positionToMoveTo

Are you trying to rotate over time? Otherwise you donâ€™t need to set rotation every iteration of the coroutine.

This would interpolate the Z axis only:

``````Quaternion from = transform.rotation;
Vector3 eulers = transform.rotation.eulerAngles;
Quaternion to = Quaternion.Euler(eulers.x, eulers.y, positionToMoveTo.transform.rotation.eulerAngles.z);

transform.rotation = Quaternion.Slerp(from, to, (elapsedTime / timeToMove));
``````