0
\$\begingroup\$

I have the camera set to go behind the object called "cameraS" and set it's x rotation to that object, and to move forward and backward along the axis behind the object. For some reason, when it moves backward, it also moves to the right and rotates to the left.

    public Transform target;
public Vector3 offset;
public float sensitivity;
private float horizontal;
private float desiredYAngle;
public Vector3 zoom;
private Quaternion rotation;
// Start is called before the first frame update
void Start()
{
    offset = target.position - transform.position;
    zoom = new Vector3(0f, 0f, 0f);
}

// Update is called once per frame
void LateUpdate() {
    horizontal = Input.GetAxis("Mouse X") * sensitivity;
    zoom -= new Vector3(Input.GetAxis("Mouse Y") * sensitivity * 0.5f, 0f, 0f);
    if(zoom.x < 0){
        zoom = new Vector3(0, 0, 0);
    }
    if(zoom.x > 20){
        zoom = new Vector3(20, 0, 0);
    }
    target.Rotate(0, horizontal, 0);
    desiredYAngle = target.eulerAngles.y;
    rotation = Quaternion.Euler(0, desiredYAngle, 0);
    transform.position = target.position - (rotation * (offset + new Vector3(offset.x - zoom.x, 0f, 0f)));
    transform.LookAt(target);
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.