- Home /

# matching rotation of one object to another on 1 axis only

hi all, ive got a camera gimbal - one object moving on the y axis, and another object with camera (attached by hinge) moving in the x axis, creating a camera 'gimbal', which i move with add torque to their respective rigid bodies for a realistic movement, which works perfectly. i also have a mini map, with a top and a side view (this is a space game in a galaxy so top and side view provide 3d context of location and rotation in space). each of the top and side views in the mini map have a pointer that reflects the location and rotation of the camera location: here is a code snippet i use to get the top view pointer to match the rotation of the camera gimbal on the cameras y axis, which translates to the pointers (UI) z axis, which works perfectly:

```
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class getlocation : MonoBehaviour
{
RectTransform topmap;
galaxy2 galaxy2;
GameObject camerasphere;
RectTransform rt;
float scale;
void Start()
{
rt = GetComponent<RectTransform>();
topmap = GameObject.Find("blank for galaxytop").GetComponent<RectTransform>();
galaxy2 = transform.root.GetComponentInChildren<galaxy2>();
camerasphere = GameObject.Find("camerasphere");
scale = topmap.rect.width / (galaxy2.maxx+galaxy2.maxz);
}
void Update()
{
Vector3 currentpos = new Vector3(camerasphere.transform.localPosition.x*scale, camerasphere.transform.localPosition.z*scale,0);
rt.localPosition = currentpos;
Vector3 temprotation = new Vector3(rt.eulerAngles.x,rt.eulerAngles.y,-camerasphere.transform. eulerAngles.y);
rt.localRotation = Quaternion.Euler( temprotation);
}
}
```

the problem arises, however, when i use a similar (near identical) code to get the camera gimbals x axis to translate into the side map views pointers z axis. the pointer doesnt follow the cameras x axis all the way round but only bouces between 180 degrees = 90 + and 90 -. it wont go 360 degrees round, which doesnt make any sense because the same basic code works perfectly from the camera y axis to the pointers z axis. here is the code to get the rotation (and location) of the camera x axis:

```
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class setlocation2 : MonoBehaviour
{
galaxy2 galaxy2;
GameObject camerasphere;
GameObject csg;
RectTransform rt;
RectTransform sidemap;
float scale;
void Start()
{
galaxy2 = transform.root.GetComponentInChildren<galaxy2>();
camerasphere = GameObject.Find("camerasphere");
csg = GameObject.Find("cameraspheregalaxy");
rt = GetComponent<RectTransform>();
sidemap = GameObject.Find("blank for galaxyside").GetComponent<RectTransform>();
scale = sidemap.rect.width / (galaxy2.maxx+galaxy2.maxz);
}
// Update is called once per frame
void Update()
{
Vector3 currentpos = new Vector3(camerasphere.transform.localPosition.z*scale,camerasphere.transform.localPosition.y*scale,0);
rt.localPosition = currentpos;
Vector3 temprotation = new Vector3(0,0,csg.transform. eulerAngles.x);
rt.localRotation = Quaternion.Euler( temprotation);
}
}
```

can anyone help please!?!! I am missing something or could this be a bug?

any help appreciated

many thanks

paul.

Not sure if the rt.eulerAngles on the y and x axis are always supposed to be 0.0f, but the below will just keep it the same. Not sure if that’s the issue, but sometimes it’s these small things that escape us from time to time.

```
Vector3 temprotation = new Vector3(rt.eulerAngles.x,rt.eulerAngles.y,csg.transform. eulerAngles.x);
rt.localRotation = Quaternion.Euler( temprotation);
```

**Answer** by gaiastellar
·
Jun 04, 2019 at 03:20 PM

in answer to my own question, if anyone else has this problem this might help. i actuall ythink this is a bug and will be reporting it.

i created a ui to diaply the x,y and z euler angles of an object being rotated. when the object is rotated all the way round, as expected the y and z axis display values from 0-360 degrees and then back to zero. the x axis however does not do this. as the object is rotated on its x axis all the way round, it goes from 0-90, then back down from 90-0 again, and then it goes 270-360, and then from 360 -270. it completely misses 90-270 degrees. there is no way to get the object to register x axis degrees 90-270. obviously a bug. read this post: click here for a post that has the same problem and scroll right down to the end for a solution where someone has written their own method for converting quaternions to euler angles, which actually works as expected.

I don’t think this is a bug. Since unity just does rotations with quaternions internally, then sets the Euler values doesn’t mean it will be what you expect. What you are seeing is likely correct. XYZ of a quaternion is the direction the axis is pointing and W is the angle of the rotation, this means that you can get the exact same result with different inputs. If the direction of the axis is (1,0,0) (world up) and the angle was -1 in radians, the result will be the exact same as (-1,0,0) (world down) with an angle of +1 radians. When this information gets translated to a Euler angle it usually looks exactly how you have described it because quaternions only have angles equal to 180 degrees max. So you will see rotations like +180 to -180 because -180 is equal to +180 just as -90 is equal to +270 so rotations from -90 to +270 actually make perfect sense if you think in quaternions. That’s why it is recommended to do as much as you can in quaternions as Euler angles have gimbal lock and Unity using quaternions internally to calculate rotations is likely simply an effort to avoid gimbal lock.

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