Camera stop when mouse is not moving

So, i did a script for the camera to follow my crosshair around (Im doing a topdown shooter) and I sorta want the effect of the camera following the crosshair as it moves just like in enter the gungeon, everything is right and i did a script for the camera to follow the crosshair but, even when my mouse is not moving, the camera keeps going forward, or better said, transforming to where its positioned to. I want the camera to stop moving if my mouse isnt moving, AND i also want to but a border so that you cant go that far enough so the player doesnt get out of the picture, so like a border that follows the player around, any help appreciated thanks.

Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public classCameraCrosshair : MonoBehaviour
{

public Transformcrosshair;
private bool mouseismoving = false;

voidStart()
{
    mouseismoving=false;
}


voidUpdate()
{

     if   (mouseismoving=true)
     {
       transform.position=newVector3(crosshair.position.x,crosshair.position.y,transform.position.z);
      }

   }

}

Hi,
In the above example you are confusing the assignment operator (=) with the equality operator (==).

if(mouseismoving=true)

is always assigning the value of true to mouseismoving. Therefore that condition will never be false.

this:

if(mouseismoving == true)

or more correct:

if(mouseismoving)

will give you the expected result.

Then you only need to test that the mouse is moving and assign that value to mouseismoving, from the top of my head, something along the lines of:

float mouseMagnitude = new Vector3(Input.GetAxis(“Mouse X”), 0, Input.GetAxis(“Mouse Y”)).magnitude;
mouseismoving = mouseMagnitude > 0f;

I’m not sure what you mean by border, do you mean a limit on how far the camera can travel away from the player?

If so then all you need is to get the player position and a Vector3 that describes how far in any direction the camera is allowed to travel. Then all you need to do is do a test to see if that cameras transform.position is still within that box. If not then dont allow the moment or move the camera back into the box. I believe this is called an AABB test if you want to search for specific code to do this.

A more simple approach would be to define a max distance from the player (for example 10f). And then test if Vector3.Distance(playerPosition, cameraPosition) < maxDistance . If it is then allow movement, if not then disallow the movement. This will result in a circular border however.

Be aware that unless the camera is a child of player, you will need to move the camera back into range if the player moves away from the camera.

I hope that makes sense.