Camera position incorrect when tilted

Hi (new to Cesium) Im currently running into an issue in my code that I cant seem to figure out. My project currently uses a cesium widget and not a viewer. Long story short, we cant simply migrate to a viewer because of cascading issues. So, I have to recreate the trackedEntity property that the viewer supports. I have a 3D model that Im tracking on the map. The model is frequently updated every second and, im storing its updated position in a variable called “cartesian3”. I need to lookAt the model when it travels and, I currently call a function that contains this logic:

        if (cartesian3) {
            const heading = this.scene.camera.heading;
            const pitch = this.scene.camera.pitch;
            const height = this.scene.camera.position.z;
            const hpr = new Cesium.HeadingPitchRange(heading, pitch, height);
            this.scene.camera.lookAt(cartesian3, hpr);
        }

When looking top down or in 2D, the logic works flawlessly. The issue is that when I tilt the map Im looking at the model. sometimes it zooms in slightly. But everytime on release, the camera is thrown out to see the globe. I’ve noticed that the camera position is constantly different and, I’m unsure why because Im not setting the view. I’m just calling lookAt while tilted and everything goes haywire.

Hi @madison-boman, can you use a different value for the range?

The “range” value in HeadingPitchRange is the distance between the camera and the object you are looking at. If the model is on the surface (height = 0), and you are looking straight down, then the range will be equal to camera.position.z. But if you are looking at an angle, the range will be larger than the height. So every call to the function will change the distance.

Perhaps you could store the initial distance as a constant, then use that same value on every update.