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;
}