LookAt problem

My camera orbits around an object. The LookAt function changes all x,y and z axis values at 90 degrees and 270 degrees. I want a smooth view but the camera flips suddenly. How can I solve this problem? (I’m in space. There is no up or down.)

My code is like this:

    public Vector3 DistanceVector = Vector3.zero;
    public float Distance = 5f;

    void Update()
    {
        // get object to camera vector
        DistanceVector = transform.position - Target.position;

        if (Input.GetMouseButton(1))
        {
            //rotate camera
            DistanceVector = Quaternion.Euler(5f, 0, 0) * DistanceVector;
        }

        // move camera to calculated position
        DistanceVector = DistanceVector / DistanceVector.magnitude * Distance;
        transform.position = Target.position + DistanceVector;

        // look at the object
        transform.LookAt(Target);

    }

any suggestions?

transform.LookAt() also takes a second optional parameter that indicates which way the up (green) axis should be twisted. By default it is Vector3.up, so thats why your object flips to keep it’s green axis directed towards world up vector.

You can probably solve the problem by:

transform.LookAt(Target, transform.up);
3 Likes

it worked. thank you.

thanks