scroll screen view with the mouse

Hi everyone,

I am really a beginner, need my view to scroll when the mouse reaches the border of the screen. Here is how my script looks like . It works but I would like that the scrolling stops whn the mouses reached the border of the screen. how do I do that? thanks a lot for your help!

#pragma strict

public var Boundary : int = 50;
public var speed : int = 5;
public var scrollingAllowed: boolean = false;

private var theScreenWidth : int;
private var theScreenHeight : int;

function Start()
{
     theScreenWidth = Screen.width;
     theScreenHeight = Screen.height;
}
public function Update()

{if (scrollingAllowed == true)

     {if (Input.mousePosition.x > theScreenWidth - Boundary)
     {
        transform.position.x += speed * Time.deltaTime; // move on +X axis
     }
     if (Input.mousePosition.x < 0 + Boundary)
     {
        transform.position.x -= speed * Time.deltaTime; // move on -X axis
     }

     }
     }


  public function allowScrolling()

{
     scrollingAllowed = true;
}

You’re contradicting yourself in your explanation. Could you explain in more detail the behavior you’re trying to make?

Also please use Code Tags when pasting code.

Ah yeah sorry,
I meant that I would like the scrolling to stop when I have reached the end of my background.
I will be careful now with pasting the code,

thank you!

One simple way to do this would be to add a new SpriteRenderer variable, to act as your “scrollable area” or something.

Then you can use SpriteRenderer.bounds.max and min to clamp your camera’s position inside of it.

I would use LateUpdate to clamp it. I don’t write Unity Javascript so forgive me if this has errors or something.

I did some conversion to get the world-unit size of the camera. I have not tested this.

public var backgroundSprite : SpriteRenderer; // the sprite renderer to use for boundaries
private var maxScrollPosition : Vector2; // the maximum position for the camera
private var minScrollPosition : Vector2; // the minimum position for the camera
private var cameraSize : Vector2; // the world-space size of the camera (not pixels)

public function Start() {
    // viewport space goes from (0,0) to (1,1)
    // position of the bottom left corner of the camera
    Vector3 min = Camera.main.ViewportToWorldPoint(Vector3.zero);
    // position of the top right corner of the camera
    Vector3 max = Camera.main.ViewportToWorldPoint(Vector3.one);

    // get the distances between max and min on the x and y axes
    // to find how big the camera is in the world
    cameraSize.x = max.x - min.x;
    cameraSize.y = max.y - min.y;

    // for the maximum position, start with the top right corner of the background
    maxScrollPosition = backgroundSprite.bounds.max;

    // subtract half of the camera size in both directions
    // this gives you the position where the camera is perfectly in the upper right corner of the sprite
    maxScrollPosition.x -= cameraSize.x * 0.5f;
    maxScrollPosition.y -= cameraSize.y * 0.5f;

    // for minimum position, start with the bottom left corner of the background
    minScrollPosition = backgroundSprite.bounds.min;

    // add half the camera size in both directions
    // this gives you the position where the camera is perfectly in the bottom left corner of the sprite
    minScrollPosition.x += cameraSize.x * 0.5f;
    minScrollPosition.y += cameraSize.y * 0.5f;
}

// called after Update, before rendering (so adjustments will not be visible to the player)
public function LateUpdate() {
    // keep this object's position within the min and max for both axes
    transform.position.x = Mathf.Clamp(transform.position.x, minScrollPosition.x, maxScrollPosition.x);
    transform.position.y = Mathf.Clamp(transform.position.y, minScrollPosition.y, maxScrollPosition.y);
}

Another method would be to check before actually moving the camera, and then only move the camera to the min or max if it would otherwise over-shoot the edge.

Thank you! I am not sure I understood everything because I am really a beginner but I will try that tonight! Thank you again!

You’re welcome, feel free to ask questions, and let me know if you managed to apply this to your existing code. I’ll add some more comments to the code to explain it.