Camera flickers back and forth between 2 points?

I’ve looked through all the similar questions and I could not find a solution for this. Basically, I wanted to write a script to click and drag the camera around the map that I’ve created. The camera moves inverse to the mouse drag to mimic the map moving when dragging the mouse button. For some reason every time I start to drag the mouse, the camera flickers between 2 points, roughly 30 times a second.

Any help to fix the issue would be greatly appreciated!

Below is the method which is in the Update() function that is supposed to move the camera around. The script is attached to the camera and there is no other camera script in any of my objects.

void MoveCameraMouse()
    {
        
        //if left mouse button is clicked, grab starting mouse position 
        if (Input.GetMouseButtonDown(0))
        {
            cameraPos = this.transform.position;
            RaycastHit hit;
            Ray ray = UnityEngine.Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))
            {
                if (hit.collider != null)
                {
                    hitPointOrigin = hit.point;
                    Debug.Log("starting point" + hit.point);
                }
            }
        }
      
        //if mouse button is held down but not clicked during this frame
        if (Input.GetMouseButton(0))
        {
            RaycastHit hit;
            Ray ray = UnityEngine.Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))
            {
                if (hit.collider != null)
                {
                    hitPointEnd = hit.point;
                    Debug.Log(hit.point);
                }
            }

            Vector3 move = new Vector3(cameraPos.x - (hitPointEnd.x - hitPointOrigin.x), cameraPos.y, cameraPos.z - (hitPointEnd.z - hitPointOrigin.z));
            transform.position = move;
            
        }
    }

The problem is that your hitPointEnd starts pingponging back and forth as soon as the camera is moved:

if you place the mouse over point A (which gets stored in hitPointOrigin), and then you move the mouse to point B, on the next frame the camera will move to place point A directly under the mouse (which is what you wanted). However, now the raycast says that the mouse is over point A (because the camera moved), and now hitPointEnd and hitPointOrigin will both be the same, and the camera will, on the next frame, snap back to the original position, which will put point B back under the mouse, and it will flip back and forth every frame.

I think if I were trying to implement this functionality, I would do a ray intersection with a virtual plane aligned with your map that was attached to the camera itself.

Alternatively (if your maps if perfectly flat), you could still use your approach, but instead of comparing the current hit point to the original hitPointOrigin, you’d keep track of the last hitPoint instead of the original, and only offset the camera by that small amount per frame.

In mousebuttondown:

hitPointLast = hit.point;

In mousebuttondrag:

//...
hitPointCurrent = hit.point;
Vector3 mouseMove = hitPointCurrent-hitPointLast;
transform.position=transform.position-mouseMove ;
hitPointLast = hitPointCurrent;    // <--- put current hit point in last hip point for the next frame

btw you don’t need to separate out the coordinate x,y,z in your vector math like that. There are builtins so you can simply add and subtract vectors (as in my code).