Simple problem I can't seem to figure out, locking camera to mouse

So in my 2d isometric game, I’m implementing a camera that simply follows the player at all time unless space is held down, in which case it should lock onto where the mouse is.

I have something like this,

    void Update()
    {
        if (Master.mainPlayer==null) {

            return;
        }

        Keyboard keyboard = Keyboard.current;

        if(keyboard.spaceKey.isPressed)
        {
           var vec= cam.ScreenToWorldPoint(Mouse.current.position.ReadValue());
            this.gameObject.transform.position =  new Vector3(vec.x, vec.y,-10f);

        }
        else { 
        this.gameObject.transform.position =  new Vector3(Master.mainPlayer.transform.position.x,Master.mainPlayer.transform.position.y,-10f);

        }
    }

but of course since moving the camera changes the position of the mouse relative to the world, this just causes the camera to fly in the direction of the mouse rather than locking on. For some reason I can’t quite figure out the correct logic to such a simple problem

There is no solution to the problem as you’ve implemented it.
Perhaps you want to maintain the current offset to the position you’re clicking, and keep it at the same position on screen?
There are number of ways to do that, but you could cache the difference between your current point and the new world point when you first click, and add that to the new point while you’re holding.

you should invest some time to familiarize yourself with cinemachine because it has the functionality to change the target with ease smoothly change the position (which is what I think you are saying you want)

or you can try implementing the ease yourself using this free asset

I don’t want to tween between anything. I’m saying I just want to find a way to center the camera around the mouse, and moving the mouse will pan it around the world. There’s no way to do this???

Locking it to the mouse is not the correct way to think about this. Instead, you want to translate the pointer’s delta movement into an offset, which you apply to the camera’s position with respect to the player. Naturally you’ll want to clamp this offset into a circle around the player.

Alternatively you could get the difference between the cursors position and the centre of the screen and translate that into an offset.

This same system could also apply to controllers as well.