# scrolling a 2D scene

My 2D game has a large map. I want the player to scroll the map using his finger. I have already done simple version using Touch.

But I have seen games where you can flick the finger and the camera goes little distance and stops. And if it reaches the edge it bounce around.

Can anyone suggest how to do something like that?
Do we add a physics component to the camera?

This is the code I have…

``````if (Input.GetMouseButtonDown(0))
isMousePressed = true;
else if (Input.GetMouseButtonUp(0))
isMousePressed = false;

if (isMousePressed && Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
currentOthoSize   = Camera.main.orthographicSize;

float authoFactor = currentOthoSize / originalOthoSize;

Touch touch       = Input.GetTouch(0);
Vector3 diff      = -touch.deltaPosition * touch.deltaTime * authoFactor * scrollFactor;

}

{

float possibleWidth      = currentOthoSize * Camera.main.aspect;

float possibleTop        = originalOthoSize - currentOthoSize;
float possibleBottom     = currentOthoSize - originalOthoSize;
float possibleLeft       = originalLeft + possibleWidth;
float possibleRight      = originalRight - possibleWidth;

cameraTransform.position = new Vector3(Mathf.Clamp(cameraTransform.position.x + midPoint.x, possibleLeft, possibleRight), Mathf.Clamp(cameraTransform.position.y+ midPoint.y, possibleBottom, possibleTop), cameraTransform.position.z);
}
``````

I’ve written similar code a few times. It doesn’t require physics. You need to save the velocity of the finger movement from the current frame. It will be the delta of the frame move divided by the delta time. When the finger is lifted, you continue to move at the same velocity. Each frame you reduce the velocity. In FixedUpdate() you can do:

``````velocity = velocity * 0.985;
``````

If you hit an edge, you reverse the velocity for that component. So if you hit a side:

`````` velocity.x = -velocity.x;
``````

Without your code, it is hard to be more specific.