How to clamp the values of screen edge camera movement?

I have an RTS style camera script im using to move the camera when you move the mouse to the edges of the screen So my question is How do I stop it from going past the play area and into the void so to speak? this is a snippet of my code…

   if (AllowScreenEdgeMove && (!_rtsCamera.IsFollowing || ScreenEdgeMoveBreaksFollow))
        {
            var hasMovement = false;

            if (Input.mousePosition.y > (Screen.height - ScreenEdgeBorderWidth))
            {
                hasMovement = true;
                _rtsCamera.AddToPosition(0, 0, MoveSpeed * Time.deltaTime);
            }
            else if (Input.mousePosition.y < ScreenEdgeBorderWidth)
            {
                hasMovement = true;
                _rtsCamera.AddToPosition(0, 0, -1 * MoveSpeed * Time.deltaTime);
            }

            if (Input.mousePosition.x > (Screen.width - ScreenEdgeBorderWidth))
            {
                hasMovement = true;
                _rtsCamera.AddToPosition(MoveSpeed * Time.deltaTime, 0, 0);
            }
            else if (Input.mousePosition.x < ScreenEdgeBorderWidth)
            {
                hasMovement = true;
                _rtsCamera.AddToPosition(-1 * MoveSpeed * Time.deltaTime, 0, 0);
            }

            if (hasMovement && _rtsCamera.IsFollowing && ScreenEdgeMoveBreaksFollow)
            {
                _rtsCamera.EndFollow();
            }
        }

Theoretically you have some way knowing what the boundaries of the level are. For simplicity’s sake, I’m going to make them variables that you have the designer set in the editor, but you could also hard code them in the script or get them dynamically by testing the extents of one or more objects in the scene. In fact, you have probably already done this work to prevent units from following movment commands into the ether.

So in your class variables, you would have something like:

public float camMaxX;
public float camMinX;
public float camMaxZ;
public float camMinZ;

Again, in my example, the designer is expected to set these.

Then, I would use these values in your AddToPosition Function at the very end of the update.

if (transform.position.x > camMaxX){
    transform.position = new Vector3( camMaxX, transform.position.y, transform.position.z);
}

You would follow similar logic for the X min, and for the Zs.

Hope that helps!