- Home /
C#. Rotation Help!
In my code the camera must stick to the Y Rotation of another object. The other object is known as target. target = theobject.transform. What code should I use to make the camera have the same Y rotation as theobject (target)? Looked everywhere and Eulers are confusing me!
If you are not looking at the target but just need to get the rotation of the target simply put this line in your camera update script:
transform.rotation.y = target.transform.rotation.y;
Don't assign values directly to transform.rotation like that! Rotation is a quaternion, a mysterious creature whose XYZW components have nothing to do with the nice angles we see in the Inspector's Rotation field (they are actually transform.eulerAngles). You should ins$$anonymous$$d copy transform.eulerAngles, but this wouldn't work fine either (see my answer)
Answer by Visal · Dec 17, 2013 at 04:23 AM
Ok you want the rotation y of the camera to has the same value as target's rotation y value. In that case try this:
public Transform target;
void LateUpdate()
{
Vector3 camEular = transform.rotation.eulerAngles;
transform.rotation = Quaternion.Euler(camEular.x, target.rotation.eulerAngles.y, camEular.z);
}
Note: attach this script to Camera
Hope that could help :)
Answer by aldonaletto · Dec 15, 2013 at 03:09 PM
Euler probably won't help you: using a single transform.eulerAngles angle isn't reliable, since the 3 angles (XYZ) may change to weird (although correct) combinations. A more reliable solution is to copy the object's forward direction (camera script):
transform.forward = target.forward;
Or make the camera look in the same direction while keeping its "head" up:
transform.rotation = Quaternion.LookRotation(target.forward, Vector3.up);
eulerAngles also has another problem (which is the reason Quaternions are used in the first place) in Gimbal Lock. It is possible to rotate with a Euler representation such that you lose a degree of freedom (that is, it becomes temporarily impossible to rotate in a certain direction). For this reason as well, it is best to avoid using Euler angles at all.
Answer by Sindorej · Dec 15, 2013 at 03:06 PM
Vector3 rot = YOUROBJECT.transform.rotation.eulerAngles;
transform.rotation.eulerAngles = new Vector3(transform.rotation.eulerAngles.x,rot.y,transform.rotation.eulerAngles.z);
Your second line won't work since a Quaternion is a struct and not a reference type.
Transform has an additional property for eulerAngles. This would work just fine.
Another way is this:
transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, rot.y, transform.rotation.eulerAngles.z);
However changing eulerangles this way usually just causes problems. Without a more detailed explanation of the problem it's hard to give the "correct" answer
Answer by hans_vesthardt98 · Dec 15, 2013 at 04:17 PM
I guess you can try something like this: (Untested)
pubic Transform: cam;
void Update(){
Vector3 camP = cam.rotation;
camP.y = target.transform.rotation.y;
cam.rotation = camP;
}
Uhm first your code looks like a mixture of C#, UnityScript and Boo. At least it wouldn't compile in any of those languages.
Second, he wanted to align the rotation, not the position. I think you got the question wrong ;)
Well, i have exactly that code on my game and it works. I meant to write rotation ins$$anonymous$$d of position, that is why i wrote untested. But it should work no problem ;)
Edited the code now, i would recommend Strideynet to try this code, as it works for me.