I’ve been messing with this for a week now and have made absolutely no progress…
I’m making a 2D board game, the game board is situated such that The Main (orthographic) Camera looks down in the Y axis to see the face of the board. So looking at the board the x axis is left/right (west/east) and the z axis is top/bottom (north/south). The y axis is not used except to initially set the height (distance) of the camera from the board and should never be changed.
The game board is like a large map in that it is bigger than the orthographic camera’s size so I have to move the camera around to see the parts of the board that are outside the view of the camera.
I’m trying to swipe on my iPad’s screen to move the camera across the board. A swipe to the left moves the camera to the left to show the west part of the board, and visa versa.
But I’ve tried many things and due to my lack of understanding of Unity’s cooridnate systems and corresponding functions I can’t accomplish what I want to do.
The program below makes sense to me, but itdoesn’t work as a short swipe will send the camera to lever-never land. I have three questions:
-
What am I doing wrong?
-
Do ScreenToWorldPoint and WorldToScreenPoint work when the camera is set to orthograaphic mode?
-
Won’t ScreenToWorldPoint and WorldToScreenPoint modify the y axis too even though I don’t want the y axis to be changed?
*NOTE: selectedCamera is a public variable with the Main Camera dropped into it via the Inspector.
// Get current camera pos in world coord and convert to screen x,y.
Vector3 ccsp = Camera.main.WorldToScreenPoint(selectedCamera.transform.position);
// Get swipe delta on screen
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
// Adjust camera's screen coord according to delta
if(touchDeltaPosition.x < 0){
ccsp.x -= Mathf.Abs(touchDeltaPosition.x);
}else if(touchDeltaPosition.x > 0){
ccsp.x += Mathf.Abs(touchDeltaPosition.x);
}
if(touchDeltaPosition.y < 0){
ccsp.z -= Mathf.Abs(touchDeltaPosition.y);
}else if(touchDeltaPosition.y > 0){
ccsp.z += Mathf.Abs(touchDeltaPosition.y);
}
// convert camera's adjusted screen position back to world coords
Vector3 newCcsp = Camera.main.ScreenToWorldPoint(new Vector3(ccsp.x, 0, ccsp.z));
// and move camera to the new world position.
selectedCamera.transform.Translate(newCcsp, Space.World);
I want to throw in 2 cents real fast. If I swipe, I want it to pan, which means you'd move the camera the opposite direction of the swipe (the texture under the finger would stay under the finger during the swipe). So at the very least, maybe give an option to flip swip axis in your preferences.
– Dracoratgood thinking, @drac
– Fattie