Hello all.
So, I’ve created an area map in the game that I’m working on, using a top down, orthographic camera. Now, I have implemented touch scrolling with the following code:
//see if player scrolled the map
private void HandleScroll()
{
if (Input.touchCount == 1 Input.GetTouch(0).phase == TouchPhase.Moved)
{
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
float x = touchDeltaPosition.x * scrollModifier;
float y = touchDeltaPosition.y * scrollModifier;
camera.transform.Translate(-x, -y, 0);
}
}
Now, besides some precision issues (which I can deal with later), it works just fine, except for one problem: it doesn’t stay with a boundary.
The way I wanted to implement this was to essentially create an invisible plane object to act as the boundary, and then when running the touch logic, check to see if the camera is outside of the boundary, and if so, correct it. The problem I’m having is conceptualizing how to determine this.
For instance, my first thought was to get the destination position of the touch, and do the checking there, but then I realized that I really need to know if the camera view itself (viewport?) is within the boundary. So, I need to be able to calculate the following:
-
The world space coordinates of each side of the camera camera (north, south, east, west). However, how can I do this if all I have is the coordinates of where the camera will be?
-
If I can get 1), then I can calculate if it’s outside of the boundary.
-
If say the east side of the camera view ends up at 260.0, and the boundary is at 250.0, I need to calculate where the camera itself should be moved to, relative to the 250.0 boundary point.
Any ideas/thoughts/questions/comments would be greatly appreciated.
Thank you.