Map level selection slide smoothly

Hello everyone. I want to do with my map levels selection slide smoothly after dragging the mouse or finger on the screen. I have this code but it doesn’t work the way I want to, so he’s shutting down without sliding smoothly after sliding the finger on the screen of the phone.

Explaining best: what I want to do is that the camera continues to slide smoothly after the drag of the mouse. Similar to map levels selection Candy Crush. This code I have not continued to slide after the lifting of the mouse or touch, the camera immediately. The code below is added to the camera

  private Vector2 _prevPosition;
        private Transform _transform;

		public Camera Camera;
        public Bounds Bounds;

        public void Awake()
        {
            _transform = transform;
        }

        public void OnDrawGizmos()
        {
            Gizmos.DrawWireCube(Bounds.center, Bounds.size);
        }

        public void Update()
        {

#if (UNITY_ANDROID || UNITY_IPHONE) && !UNITY_EDITOR
			HandleTouchInput();
#else
			HandleMouseInput();
#endif
        }

		private void HandleTouchInput()
		{
         	if(Input.touchCount == 1)
			{

				Touch touch = Input.GetTouch(0);
				if(touch.phase == TouchPhase.Began)
				{
					_prevPosition = touch.position;
				}
				else if(touch.phase == TouchPhase.Moved)
				{
					Vector2 curPosition = touch.position;
					MoveCamera(_prevPosition, curPosition);
					_prevPosition = curPosition;
				}
			}
        }

		private void HandleMouseInput()
		{
			if (Input.GetMouseButtonDown(0))
				_prevPosition = Input.mousePosition;
			
			if (Input.GetMouseButton(0))
			{
				Vector2 curMousePosition = Input.mousePosition;
				MoveCamera(_prevPosition, curMousePosition);
				_prevPosition = curMousePosition;
			}
		}

        private void MoveCamera(Vector2 prevPosition, Vector2 curPosition)
        {
            if(EventSystem.current.IsPointerOverGameObject(-1)) return;
			SetPosition(
				transform.localPosition + 
			    (Camera.ScreenToWorldPoint(prevPosition) - Camera.ScreenToWorldPoint(curPosition)));
        }

		public void SetPosition(Vector2 position)
		{
			Vector2 validatedPosition = ApplyBounds(position);
			_transform.position = new Vector3(validatedPosition.x, validatedPosition.y, _transform.position.z);
        }

		private Vector2 ApplyBounds(Vector2 position)
		{
			float cameraHeight = Camera.orthographicSize*2f;
			float cameraWidth = (Screen.width * 1f/Screen.height)*cameraHeight;
			position.x = Mathf.Max(position.x, Bounds.min.x + cameraWidth/2f);
			position.y = Mathf.Max(position.y, Bounds.min.y + cameraHeight/2f);
			position.x = Mathf.Min(position.x, Bounds.max.x - cameraWidth/2f);
			position.y = Mathf.Min(position.y, Bounds.max.y - cameraHeight/2f);
			return position;
		}

Clarify what isn’t working, this doesn’t make any sense:

I have this code but it doesn’t work the way I want to, so he’s shutting down without sliding smoothly

The first issue I can see is that doubling the camera size does not give you it’s height:

float cameraHeight = Camera.orthographicSize*2f;

And you later divide this value by 2f in the ApplyBounds() function, so why are you even doubling it in the first place?

and this line is redundant:

float cameraWidth = (Screen.width * 1f / Screen.height) *cameraHeight;

Basic math: (w * 1/h) *h will always give you back w, just do Screen.width

I’d start with cleaning out these basic errors in math/logic, then come back to explain what the problem you’re seeing is.

Think about the behavior you want: after the release of the mouse, it keeps sliding at the same speed, and then slowly decreases speed to zero.

In order to achieve this, you need to know the speed at which it was sliding to begin with. Since you’re already storing previous positions of the mouse every frame, this should be easy. When the mouse is released, calculate the velocity of the mouse in that frame:

velocity = (current - last) / deltaTime

Then each frame when the mouse is up, apply that velocity:

SetPosition( current_position + velocity * deltaTime )

And then slowly smooth the velocity to zero (smoothing value of about 0.5, probably):

velocity = Vector3.Lerp( velocity, Vector3.zero, deltaTime / smoothingValue )

Hope that answers your main concern.

You can use ScrollRect on the container of your level selection objects, it will do what you want automatically:

http://docs.unity3d.com/Manual/script-ScrollRect.html