- Home /
How can touch input directly control an orthographic camera?
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);
Answer by Fattie · Apr 10, 2013 at 09:46 AM
incredibly detailed answer here
http://answers.unity3d.com/questions/292333/how-to-calculate-swipe-speed-on-ios.html
It can take a long time to master this sort of thing. yes the many coord systems are confusing. there is no quick or easy solution.
tip use Debug.DrawLine constantly to help you. when you are working basically every other line of code is Debug.DrawLine (For future ref, here's the doco for Debug.DrawLine
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.
Your answer
Follow this Question
Related Questions
Indicate with markers the direction of Gameobjects which are outside of the camerafield. 0 Answers
How to track a world object in a Screen Space - Camera Canvas 1 Answer
What makes Camera.ScreenToWorldPoint return different results with same input? 0 Answers
WorldToViewportPoint and WorldToScreenPoint give wrong positions when VR is enabled 1 Answer
Camera.main.ScreenToWorldPoint works within Unity editor, but not in builds 0 Answers