# Match rotation of gameobejcts in one (Y) axis

Hey guys. Might seem a simple one but i do sometimes have trouble with these kind of rotation problems.

My end goal is to make sure all my NPCs are facing the right way when they move around the scene by always facing there current waypoint in a pathfinding script. As before they were gliding around with no relative rotation.

Ive tried ta few approaches none of which work how i need. Ill share them all to hopefully get some guidance on which one (if any) is the closest the actual approach i need.

Firstly i have a seperate child object which just contantly looks at the looks at current waypoint, and then i want to set my actual rotaion on the Y axis to match that rotation. However it seems i cant do this.

``````compass.LookAt(path.vectorPath[currentWaypoint]); // Child object always Looking in the direction of the current waypoint.

transform.rotation.y = compass.rotation.y; // Set my rotaion on Y to match that object on Y.
``````

This gives an error…

Also this makes me face theat rotaion in ALL axis, when i only want the Y axis to change.

``````compass.LookAt(path.vectorPath[currentWaypoint]); // Child object always Looking in the direction of the current waypoint.
transform.rotation = Quaternion.Lerp(transform.rotation, compass.rotation, Time.deltaTime * turnSpeed); // Then set rotation to that compass look roation
``````

And this one, while almost waht i ant makes them walk backwards everywhere. But it dies however only rotate the one Axis Y.

``````Vector3 faceThisWay = transform.position - (path.vectorPath[nextWaypointDistance]);
faceThisWay.y = 0.0f;
Quaternion newRotation = Quaternion.LookRotation(faceThisWay, transform.up);
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 20);
``````

So gang, which one is right and where is it going so wrong…
Many Thanks…

compass.LookAt(path.vectorPath[currentWaypoint]); // Child object always Looking in the direction of the current waypoint.
transform.rotation.y = compass.rotation.y;

The reason this throws an error is because transform.rotation.y is a getter and has no setter.

Modifying this to work would look like this:

``````compass.LookAt(path.vectorPath[currentWaypoint]); // Child object always Looking in the direction of the current waypoint.
transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, compass.rotation.y, transform.rotation.eulerAngles.z );
``````

To lerp it I’d recommend Mathf.lerpAngle:

``````float interpolator = 0.5f;
compass.LookAt(path.vectorPath[currentWaypoint]); // Child object always Looking in the direction of the current waypoint.
transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, Mathf.lerpAngle(transform.rotation.eulerAngles.x,compass.rotation.y, interpolator), transform.rotation.eulerAngles.z );
``````

I will say this:
Using the euler angles of object is easier to use, but can cause SO MANY ANNOYING PROBLEMS. So use carefully.

@HarshadK’s answer is better as the rotation doesn’t depend on euler angles.

For the last method you should change your first line to:

``````Vector3 faceThisWay = (path.vectorPath[nextWaypointDistance]) - transform.position;
``````

And it should work.