Stumped with rotation issue.

Here's the deal:

I have a camera (`Camera`) with MouseOrbit attached to it. Then I have an object with this code attached to it:

function Update () {
    transform.rotation.y = Camera.main.transform.rotation.y;
}

As well as a simple movement script, the idea being that as the camera rotates, the object mimics this rotation. Then the movement script moves the object relative to the camera.

The problem is that it doesn't work right, and I can't for the life of me figure out why...

When testing it movement is sporadic, and definitely not camera relative, though moving the camera does have an effect on movement direction it seems pretty random.

So I tried looking at some values while I rotated the camera. When looking at the camera's rotation values while rotating it, the y axis value goes up (or down) normally, but when I look at the values for the object, they slowly go up or down like normal, and then randomly switch and start counting in the opposite direction.

For example, I am rotating the camera slowly clockwise. The y rotation values of the object will go up and up until they reach say 80 (random number), then start going down. Then back up. and down and uo.... (not to 80 every time, just to a seemingly random number)

so HELP!

TL;DR'ers: The rotation of the object the above script is applied to doesn't match the rotation of the camera is it is supposed to.

The issue is that rotation is a Quaternion, with normalized x/y/z/w parameters, so taking one from one object and applying them to another will end up with odd results

You could try:

transform.eulerAngles.y = Camera.main.transform.eulerAngles.y;

though if it starts flipping 180 degrees randomly, you may want to look into a sturdier solution, possibly something like:

Vector3 fwd = Camera.main.transform.forward;
float angle = Mathf.Atan2(fwd.x, fwd.z) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0, angle, 0);

Though that one I didn't test, so the x and z could possibly be flipped