- Home /

# 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..

**Answer** by HarshadK
·
Feb 26, 2015 at 11:47 AM

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

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

And it should work.

Yes, this is a better solution than my answer, as it doesn't depend on euler angles.

Yep that does work.. sorry i removed th comment that said it doesnt work i changed something else before posting .. The nextWaypoint should actually be currentWaypoint. different types hence the error. Thanks dudes

**Answer** by sniper43
·
Feb 26, 2015 at 11:54 AM

```
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.

Appreciate the answer. I just the other easier to understand :)

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Rotation using Unity2D 3 Answers

Local child rotation and transform.LookAt 2 Answers

child lookat another child 1 Answer

How to add a rotation to this? 1 Answer

Rotation via iTween.LookTo not working (or using it wrong) 1 Answer