- Home /
Moving the camera to a touch position
I am trying to move the position of the camera to the position of a touch, how do I got about changing my Touch.position into a position for the camera?
I don't understand your question- if you can touch a point on the screen, then the camera is already at that point? Unless you want to shoot a raycast at some collider, and then move the camera to that... Are you talking about touching a point on the ground, and focusing the camera over top of that position, like an RTS?
I want to be able to double tap the screen and zoom to the position. I have the tap zoom working but it currently only zoom to the center of the screen.
Answer by Rod-Green · Nov 08, 2011 at 02:26 AM
There are a lot of different ways you could achieve this (including raycasting - though much slower). However one method would be to determine the touch offset from center and then move the camera based upon this offset (some scale might be necessary - depending on how you've setup your camera.
As an example:
Touch touch = Input.GetTouch(0);
Vector3 offset = Camera.main.ScreenToViewportPoint((Vector3)touch.position) - (Vector3.one / 2.0f);
//3:4 screen ratio = 0.75f on Y 16:9 would be 0.5625f etc
Vector3 scale = new Vector3(1.0f, 0.75f, 0.0f);
Camera.main.transform.Translate(offset * scale);
Now again this might take some work to get the camera to move and look at the point you want (i.e. tweaking the scale till the movement is right).
There are other ways like working out the plane the target it on and calculating the distance between the camera's current lookat on that plane and move that offset.
In any case setting up a special camera rig where the camera is a child of a gameobject that is aligned to the view plane will help you simplify any translation. As you can just move this aligned gameObject and the camera will move along this plane regardless of it's orientation.
Answer by Steven-Walker · Nov 08, 2011 at 02:18 AM
If you are trying to point the camera at an object and zoom, you'll need to use raycasting to first find the 3D position of the object touched. You could then use Transform.LookAt to have the camera look at the object. To make it a bit more user friendly, you'll probably want to interpolate the movement so it's smooth. I'd use iTween to do that.
I am not trying to zoom to an object, I'm trying to move the camera around the xy. Think like tap zoo$$anonymous$$g a picture on a mobile device.
I could answer your question better if the problem/goal was made clearer. It's pretty ambiguous. It sounds like you might have a scene laid out like a map. If your camera is facing forward on the Z axis looking at a plane, you can center the camera by getting the touch point in 3D space then copying the X and Y transform to your camera, but leave the Z as is. Or exchange axis if the camera is looking top down.
I have something similar to this: http://youtu.be/3HPCHtjUc1w I have the orbit, pinch and tap zoom working fine, but I am unable to get the camera to 'shift' to where the zoom is being performed.
The video helps a lot. I would do this completely different than my initial suggestion. The camera isn't really centering on any specific object, but rather the zoom is shifting the camera towards the pinch zoom center. It's more of a trick than a precise transform.
What I'd do is calculate the center by averaging the two touch points, then use a fixed interpolation (ex touchCenter.x/Screen.width) and offset the pitch and yaw of the camera. If you multiply this by the zoom change factor, you'll get more offset the more you zoom, so it will be gradual rather than an abrupt movement of the camera.
You'll also want to set limits, otherwise the user will be able to keep turning the camera further and further away from the central object.
To make this work with your existing code, you may need to insert a parent transform so you can control the regular camera orbiting separate from the centering offsets. You'll also need to play around with the calculation to dial in the amount of sensitivity you want.
Your answer
![](https://koobas.hobune.stream/wayback/20220613020955im_/https://answers.unity.com/themes/thub/images/avi.jpg)