How to zoom main camera on single point

Background: I have a medical patient lying on an operation table. What I want to do is that when I approach the patient the main camera zooms in on the calf of the patient. So whether I approach from the head or the feet, it’ll zoom on the calf. I have the code to make it zoom smoothly when it reaches a certain point; the part I’m having trouble with is how to make it zoom in on a single point from any part of the body. Here is the code I have complete: Any help is greatly appreciated! Thanks!

using UnityEngine;
using System.Collections;

public class LegZoom : MonoBehaviour {
	
	public GameObject leg;
	public GameObject player;
	

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		
		if(((leg.transform.position.x)-(player.transform.position.x)) <= 2.6f  (((leg.transform.position.z)-(player.transform.position.z)) >= -1f||((leg.transform.position.z)-(player.transform.position.z)) <=3.4f)) {
		
			Camera.main.fieldOfView = Mathf.Lerp(Camera.main.fieldOfView, 30, Time.deltaTime*1);
		}
			
		else{
				Camera.main.fieldOfView = Mathf.Lerp(Camera.main.fieldOfView, 60, Time.deltaTime*5);
			}
		
	}
		
}

Anyone with any ideas?

I’m not sure why don’t you just move the camera where you want it to be instead of playing with a field of view? I should be easy to calculate the target position in relationship to any body part and just move it to that position.

I was trying to keep the code as simple as possible, and I didn’t know how to move the camera itself…and I still don’t.

Another problem is the camera is “attached” to the head of a person, so if I move the camera towards the point, it would come off the person, meaning the player could look behind and see himself which wouldn’t be what I want either.

if you know how to move a simple transform in-game, it’s the same, the camera has a transform, move that. You can use LookAt, Raycasting, simple or even complex math… it depends on the approach you’re willing to choose.

Thanks guys for the help. I am not overly familiar with coding and scripting in general so it’s slightly challenging for me since the project I’m working on MUST be completed in 7 weeks (this is with having no prior experience with Unity). Which is why I’m kinda asking alot of questions.

Anyway, I changed the script so that the camera moves to the point. The problem is that the camera is attached to the FirstPersonController(ie wherever the Controller goes the camera goes too) so when it moves to the point, it separates from the Controller and doesn’t go back. Any ideas on how to make the else-statement make the camera return to its original spot? Thanks!

using UnityEngine;
using System.Collections;

public class LegZoom2 : MonoBehaviour {

	public GameObject body;
	public GameObject leg;
	public GameObject player;
	public GameObject camera;

	// Use this for initialization
	void Start () {
		
	
	}
	
	
	// Update is called once per frame
	void Update () {
		
	if(((body.transform.position.x)-(player.transform.position.x)) <= 2.6f  (((body.transform.position.z)-(player.transform.position.z)) >= -1f||((body.transform.position.z)-(player.transform.position.z)) <=3.4f)) {
		
		camera.transform.position = new Vector3 (6, 9.2f,-4f);
		
	
	
		}
		else{
			camera.transform.position = camera.transform.defaultPosition;
		}
	}
}

Maybe you can switch to a different camera when you want to zoom in so your FirstPersonController stays intact. Also, you can use FirstPersonController to move (that is why it is First Person Controller - so it can move in first person :slight_smile: ). It all depends on what are the rules of your game. Like for example do you have colliders that will not let your controller move closer?

I’ve started experimenting with Instantiate to try making a camera clone and then destroying it. Main problem I’m having is all the sample Destroy codes I’ve seen are in JS and I have to stick with C#.

My FirstPersonController does in fact move; the camera is parented to it (I think), so wherever it moves the camera goes along. It presents the view as from the eyes of the player. I’m approaching the body which is on a table and I can get pretty close but since I’m doing a surgical simulation, I want to zoom in extra on the part where the surgery will be going on.

Here’s my new code so far, any suggestions to making it work perhaps?

using UnityEngine;
using System.Collections;

public class LegZoom2 : MonoBehaviour {

	public GameObject body;
	public GameObject leg;
	public GameObject player;
	public GameObject camera;

	// Use this for initialization
	void Start () {
		
		
		
	
	}
	
	
	// Update is called once per frame
	void Update () {
	
		
		
		
		
	if(((body.transform.position.x)-(player.transform.position.x)) <= 2.6f  (((body.transform.position.z)-(player.transform.position.z)) >= -1f||((body.transform.position.z)-(player.transform.position.z)) <=3.4f)) {
		
		
			
		Instantiate(camera,new Vector3(6, 9.2f, -4f), transform.rotation) as Transform;
		//camera.transform.position = new Vector3 (6, 9.2f,-4f);
		}
		else{
			Destroy(GameObject, 1);
		}
		
	}
}

I don’t think you need to instantiate a new camera all the time. But if you do then remember a reference (returned from Instantiate and Destroy it by that reference. But I would just pre-create a second camera and just switch between them.

Thanks for the advice. I just decided to put the second camera right next to the leg and turn it on and off based on my position that I already had scripted…Wasn’t exactly what I was looking for but it works well. I do have one more question on the subject: How do you smooth the transition of switching between the two cameras so it’s more of a slow zoom? I’m using the object.SetActive() to turn them on and off so I don’t know if that’s possible.

  1. Predefine positions where you need to zoom to.
  2. Start your second camera from a position of your FirstPersonController
  3. Activate second camera
  4. Lerp / move the second camera to one of the predefined zoom positions

Thanks a lot for the helps and tips! I finally have a script that works nicely for me. Here it is for future reference in case anyone is interested.

using UnityEngine;
using System.Collections;

public class LegZoom2 : MonoBehaviour {

	public GameObject body;
	public GameObject player;
	public GameObject legcamera;
	public GUITexture backbutton;

	// Use this for initialization
	void Start () {
		
		
		Screen.showCursor = true;
	
	}
	
	
	// Update is called once per frame
	void Update () {
	
		Vector3 StartPos = legcamera.transform.position;
		Vector3 EndPos = new Vector3 (6.1f, 9.1f, -4.017f);
		
		
	if(((body.transform.position.x)-(player.transform.position.x)) <= 2.6f  (((body.transform.position.z)-(player.transform.position.z)) >= -1f||((body.transform.position.z)-(player.transform.position.z)) <=3.4f)) {
		
		legcamera.SetActive(true);
		player.GetComponent<AudioSource>().enabled = false;
		legcamera.transform.position = Vector3.Lerp (StartPos, EndPos, Time.deltaTime * 2);
		//camera.transform.position = new Vector3 (6, 9.2f,-4f);
		}
		
	else {
			legcamera.SetActive(false);
			legcamera.transform.position = new Vector3(4.54f, 9.434f, -3.932f);
		}
		
	
		}
		
		
	
	}