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!

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);

Vector3 rot = YOUROBJECT.transform.rotation.eulerAngles;

transform.rotation.eulerAngles = new Vector3(transform.rotation.eulerAngles.x,rot.y,transform.rotation.eulerAngles.z);

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;
}

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 :slight_smile: