Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to zoom main camera on single point

Discussion in 'Scripting' started by thorskull, Jun 17, 2013.

  1. thorskull

    thorskull

    Joined:
    Jun 5, 2013
    Posts:
    21
    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!

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LegZoom : MonoBehaviour {
    5.    
    6.     public GameObject leg;
    7.     public GameObject player;
    8.    
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.    
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update () {
    17.        
    18.         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)) {
    19.        
    20.             Camera.main.fieldOfView = Mathf.Lerp(Camera.main.fieldOfView, 30, Time.deltaTime*1);
    21.         }
    22.            
    23.         else{
    24.                 Camera.main.fieldOfView = Mathf.Lerp(Camera.main.fieldOfView, 60, Time.deltaTime*5);
    25.             }
    26.        
    27.     }
    28.        
    29. }
     
  2. thorskull

    thorskull

    Joined:
    Jun 5, 2013
    Posts:
    21
    Anyone with any ideas?
     
  3. cdevl

    cdevl

    Joined:
    Apr 10, 2013
    Posts:
    180
    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.
     
  4. thorskull

    thorskull

    Joined:
    Jun 5, 2013
    Posts:
    21
    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.
     
    Last edited: Jun 18, 2013
  5. ar0nax

    ar0nax

    Joined:
    May 26, 2011
    Posts:
    485
    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.
     
  6. thorskull

    thorskull

    Joined:
    Jun 5, 2013
    Posts:
    21
    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!

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LegZoom2 : MonoBehaviour {
    5.  
    6.     public GameObject body;
    7.     public GameObject leg;
    8.     public GameObject player;
    9.     public GameObject camera;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.        
    14.    
    15.     }
    16.    
    17.    
    18.     // Update is called once per frame
    19.     void Update () {
    20.        
    21.     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)) {
    22.        
    23.         camera.transform.position = new Vector3 (6, 9.2f,-4f);
    24.        
    25.    
    26.    
    27.         }
    28.         else{
    29.             camera.transform.position = camera.transform.defaultPosition;
    30.         }
    31.     }
    32. }
    33.  
     
  7. cdevl

    cdevl

    Joined:
    Apr 10, 2013
    Posts:
    180
    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 :) ). 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?
     
  8. thorskull

    thorskull

    Joined:
    Jun 5, 2013
    Posts:
    21
    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?

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LegZoom2 : MonoBehaviour {
    5.  
    6.     public GameObject body;
    7.     public GameObject leg;
    8.     public GameObject player;
    9.     public GameObject camera;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.        
    14.        
    15.        
    16.    
    17.     }
    18.    
    19.    
    20.     // Update is called once per frame
    21.     void Update () {
    22.    
    23.        
    24.        
    25.        
    26.        
    27.     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)) {
    28.        
    29.        
    30.            
    31.         Instantiate(camera,new Vector3(6, 9.2f, -4f), transform.rotation) as Transform;
    32.         //camera.transform.position = new Vector3 (6, 9.2f,-4f);
    33.         }
    34.         else{
    35.             Destroy(GameObject, 1);
    36.         }
    37.        
    38.     }
    39. }
    40.  
     
    Last edited: Jun 18, 2013
  9. cdevl

    cdevl

    Joined:
    Apr 10, 2013
    Posts:
    180
    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.
     
  10. thorskull

    thorskull

    Joined:
    Jun 5, 2013
    Posts:
    21
    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.
     
  11. cdevl

    cdevl

    Joined:
    Apr 10, 2013
    Posts:
    180
    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
     
  12. thorskull

    thorskull

    Joined:
    Jun 5, 2013
    Posts:
    21
    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.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LegZoom2 : MonoBehaviour {
    5.  
    6.     public GameObject body;
    7.     public GameObject player;
    8.     public GameObject legcamera;
    9.     public GUITexture backbutton;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.        
    14.        
    15.         Screen.showCursor = true;
    16.    
    17.     }
    18.    
    19.    
    20.     // Update is called once per frame
    21.     void Update () {
    22.    
    23.         Vector3 StartPos = legcamera.transform.position;
    24.         Vector3 EndPos = new Vector3 (6.1f, 9.1f, -4.017f);
    25.        
    26.        
    27.     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)) {
    28.        
    29.         legcamera.SetActive(true);
    30.         player.GetComponent<AudioSource>().enabled = false;
    31.         legcamera.transform.position = Vector3.Lerp (StartPos, EndPos, Time.deltaTime * 2);
    32.         //camera.transform.position = new Vector3 (6, 9.2f,-4f);
    33.         }
    34.        
    35.     else {
    36.             legcamera.SetActive(false);
    37.             legcamera.transform.position = new Vector3(4.54f, 9.434f, -3.932f);
    38.         }
    39.        
    40.    
    41.         }
    42.        
    43.        
    44.    
    45.     }
    46.