Zoom in on tapped point in 3D space

Hey everyone -

I’m working on a project for class that’s a shooting gallery style game for mobile. It takes place in a 3D environment, and I’m trying to implement a mechanic where the camera to zooms into the point where you tapped on the touchscreen… I know I have to use touch.position and lookAt, and I know how to zoom, but I’m not sure how to make it all work; my main problem is that touch.position only returns a Vector2, and I need a Vector3 (x, y, and z for the position/target I’m aiming at) for lookAt. Just tossing in a zero for z doesn’t work then it zooms in on a completely different position than what I need.

Help? Thanks! =]

What you need to do is use a RayCast. This ideally will send a ray from your screen point where you touch to a 3D point. The ray has to hit a collider or rigidbody, so be sure you have those setup. They can be invisible. When the ray does hit a collider, you can use RayCastHit.point to get the 3D point, and that is where you can move your camera to.

var ray = Camera.main.ScreenPointToRay (Vector3(Touch.position.x, Touch.position.y, 0)); //construct the ray
var hitInfo : RaycastHit; //stores the information of the ray when it hits something
var distance = 100; //how far ray will go, it needs to be able to go from the camera to the colliders
if (Physics.Raycast (ray, hitInfo, distance)) {
	if(hitInfo.collider.tag == "invisibleWall") //this is optional, and can be used if you want the camera to move only when the ray hits a certain type of collider
	Camera.main.transform.position = hitInfo.point; //move camera to the point the ray hit something
}

Tell me if this doesn’t work or you need more help.

Good Luck.