How can I move the camera when the mouse reaches the edge of the screen?

Hey just tell me what I need to do. I need some guidance ahhaha sorry im not the best programmer. Thanks

Try this function.

Vector2 MouseScreenEdge( int margin ) {
   //Margin is calculated in px from the edge of the screen

   Vector2 half = new Vector2( Screen.width/2, Screen.height/2 );

   //If mouse is dead center, (x,y) would be (0,0)
   float x = Input.mousePosition.x - half.x;
   float y = Input.mousePosition.y - half.y;   
   
   //If x is not within the edge margin, then x is 0;
   //In another word, not close to the edge
   if( Mathf.abs(x) > half.x-margin ) {
      x += (half.x-margin) * ( x < 0 )? 1 : -1;
   }
   else {
      x = 0f;
   }
   
   if( Mathf.abs(y) > half.y-margin ) {
      y += (half.y-margin) * ( y < 0 )? 1 : -1;
   }
   else {
      y = 0f;
   }
   
   return new Vector2( x, y );
}

You define a margin of the edge of the screen, then this function will return a measurement on how close is the mouse to the edge.

For example, margin = 20:

Vector2 mouseEdge = MouseScreenEdge( 20 );

If mouse.Edge.x is 0 (you need to use Mathf.Approximately()), then you have not touched the left or right side of the mouse. If mouse.Edge.x is about 5, then your mouse is 15px away from the right side of the screen. If mouse.Edge.x is about -20, then your mouse is on the leftmost side of your screen. The same goes for the y-axis.

From the Vector2 returned by this function, you can do something like this:

void Update() {
   Vector2 mouseEdge = MouseScreenEdge( 20 );

   if( !( Mathf.Approximately( mouseEdge.x, 0f) ) {
      //Move your camera depending on the sign of mouse.Edge.x

      if( mouseEdge.x < 0 ) {
         //Move Left
      }
      else {
         //Move right
      }
   }
}

Code is not tested, but I checked the calculation twice; so I am quite confident that this will work.

You can translate the camera’s transform on it’s .x axis by Input.GetAxis(“Mouse X”) (which return a value from -1 to +1. And do this only if the Input.mousePosition.x is at the screen edge Screen.width
So:

var camTransform:Transform;
var speed:float;

if (Input.mousePosition.x == Screen.width){

   camTransform.position.x += Input.GetAxis("Mouse X") * Time.deltaTime * speed;
}

if (Input.mousePosition.x == 0){

   camTransform.position.x -= Input.GetAxis("Mouse X") * Time.deltaTime * speed;
}

Well, not much point in explaining then

var sensitivity = 10;

function Update(){

if(Input.mousePosition.x < sensitivity)
transform.Rotate(-Vector3.up * Time.deltaTime * Mathf.Abs(sensitivity -Input.mousePosition.x);


else if(Input.mousePosition.x > Screen.width - sensitivity)
transform.Rotate(Vector3.up * Time.deltaTime * Mathf.Abs(Screen.width -Input.mousePosition.x);

}

add this javascript to your camera (untested…)