Search Unity

Rotate Camera around Game Object and Zoom Camera into same Game Object

Discussion in 'Scripting' started by Aravindadx, Nov 6, 2018.

  1. Aravindadx

    Aravindadx

    Joined:
    Jul 29, 2018
    Posts:
    14
    Hello Friends.

    I need to know how to rotate the main camera around a game object in unity. In same time I need to zoom the camera into game object.

    Most of tutorials have those two methods separately. I've try those tutorials combine each other. Camera rotation is working perfectly for me, but when I try to zoom it works, but the game object disappear from camera because it's going to outside from view angle of camera.

    All the things i need to do around the game object. My game object is an automobile engine part. I'm developing this kind of thing for my final project in university.

    If there is a anyone who can hep me, I'm appreciate your kindness very much.

    Thank You!
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,438
    Hi,

    can you show some snippet of your current script?

    for zooming, have you tried moving camera with its transform.forward?
     
    Aravindadx likes this.
  3. Aravindadx

    Aravindadx

    Joined:
    Jul 29, 2018
    Posts:
    14
    Hello @mgear

    I'm using for following C# code to rotate the camera around the object.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityScript;
    5.  
    6. public class cameraRotate : MonoBehaviour {
    7.  
    8.     public GameObject target;//the target object
    9.     public float speedMod = 10.0f;//a speed modifier
    10.     private Vector3 point;//the coord to the point where the camera looks at
    11.  
    12.     void Start()
    13.     {//Set up things on the start method
    14.         point = target.transform.position;//get target's coords
    15.         transform.LookAt(point);//makes the camera look to it
    16.     }
    17.  
    18.     void Update()
    19.     {//makes the camera rotate around "point" coords, rotating around its Y axis, 20 degrees per second times the speed modifier
    20.         transform.RotateAround(point, new Vector3(0.0f, -1.0f, 0.0f), 20 * Time.deltaTime * speedMod);
    21.     }
    22. }

    And also I'm using following code to zoom the camera towards to object.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraMovement : MonoBehaviour {
    5.  
    6.     private float speed = 2.0f;
    7.     private float zoomSpeed = 2.0f;
    8.  
    9.     public float minX = -360.0f;
    10.     public float maxX = 360.0f;
    11.    
    12.     public float minY = -45.0f;
    13.     public float maxY = 45.0f;
    14.  
    15.     public float sensX = 100.0f;
    16.     public float sensY = 100.0f;
    17.    
    18.     float rotationY = 0.0f;
    19.     float rotationX = 0.0f;
    20.  
    21.     void Update () {
    22.  
    23.         float scroll = Input.GetAxis("Mouse ScrollWheel");
    24.         transform.Translate(0, scroll * zoomSpeed, scroll * zoomSpeed, Space.World);
    25.  
    26.         if (Input.GetKey(KeyCode.RightArrow)){
    27.             transform.position += Vector3.right * speed * Time.deltaTime;
    28.         }
    29.         if (Input.GetKey(KeyCode.LeftArrow)){
    30.             transform.position += Vector3.left * speed * Time.deltaTime;
    31.         }
    32.         if (Input.GetKey(KeyCode.UpArrow)){
    33.             transform.position += Vector3.forward * speed * Time.deltaTime;
    34.         }
    35.         if (Input.GetKey(KeyCode.DownArrow)){
    36.             transform.position += Vector3.back * speed * Time.deltaTime;
    37.         }
    38.  
    39.         if (Input.GetMouseButton (0)) {
    40.             rotationX += Input.GetAxis ("Mouse X") * sensX * Time.deltaTime;
    41.             rotationY += Input.GetAxis ("Mouse Y") * sensY * Time.deltaTime;
    42.             rotationY = Mathf.Clamp (rotationY, minY, maxY);
    43.             transform.localEulerAngles = new Vector3 (-rotationY, rotationX, 0);
    44.         }
    45.     }
    46. }
    Already the above code is move the camera with the mouse here and there when I'm drag and drop. However when I join those codes I have following result with my project.

    https://media.giphy.com/media/cmyXg2lRg7qk4VCxOE/giphy.gif

    Zooming part is not working as I expected. I need to solve that matter. Specially I need to say I'm not the author for above C# codes. I found those scripts from another authors.

    Thank You!
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,438
    can try replacing those directions with camera directions,
    Code (CSharp):
    1. if (Input.GetKey(KeyCode.RightArrow)){
    2.             transform.position += transform.right * speed * Time.deltaTime;
    3.         }
    4.         if (Input.GetKey(KeyCode.LeftArrow)){
    5.             transform.position += transform.left * speed * Time.deltaTime;
    6.         }
    7.         if (Input.GetKey(KeyCode.UpArrow)){
    8.             transform.position += transform.forward * speed * Time.deltaTime;
    9.         }
    10.         if (Input.GetKey(KeyCode.DownArrow)){
    11.             transform.position += transform.back * speed * Time.deltaTime;
    12.         }
    Vector3.forward is world space z axis direction,
    transform.forward is the local space z direction (so if script is attached to camera, it would move to that direction its looking at when press up arrow)
     
    Aravindadx likes this.
  5. Aravindadx

    Aravindadx

    Joined:
    Jul 29, 2018
    Posts:
    14
    Hello @mgear

    I've combined your C# script into my project and it is working perfectly. But your code had two line errors which is belong to line number 5 and 11. There is transform.left and transform.back according to your script. But actually in unity there is no transform.left and transform.back. So we have to replace those lines with -transform.right(left side) and -transform.forward(back side).

    After those modifications I've combined camera rotation around the object zooming to the object as follows.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityScript;
    5.  
    6. public class CameraMovement : MonoBehaviour {
    7.  
    8.     public float zoomspeed = 5.0f;
    9.     public GameObject targetobject;
    10.     public float rotatespeed = 10.0f;
    11.     private Vector3 point;
    12.  
    13.     void Start()
    14.     {
    15.         point = targetobject.transform.position;
    16.         transform.LookAt(point);
    17.     }
    18.  
    19.     void Update () {
    20.         //Rotate the Entire Camera around the relevant Object
    21.         transform.RotateAround(point, new Vector3(0.0f, -1.0f, 0.0f), 20 * Time.deltaTime * rotatespeed);
    22.  
    23.         //Zoom in and Zoom Out
    24.         if (Input.GetKey(KeyCode.RightArrow))
    25.         {
    26.             transform.position += transform.right * zoomspeed * Time.deltaTime;
    27.         }
    28.         if (Input.GetKey(KeyCode.LeftArrow))
    29.         {
    30.             transform.position -= transform.right * zoomspeed * Time.deltaTime;
    31.         }
    32.         if (Input.GetKey(KeyCode.UpArrow))
    33.         {
    34.             transform.position += transform.forward * zoomspeed * Time.deltaTime;
    35.        
    36.         }
    37.         if (Input.GetKey(KeyCode.DownArrow))
    38.         {
    39.             transform.position -= transform.forward * zoomspeed * Time.deltaTime;
    40.         }
    41.     }
    42. }
    Above C# Script it is working perfectly and anyone have to change targetobject, zoomspeed and rotatespeed. Anyone can apply this script to main camera. Within your help, now I got the solution.

    But, still I need to limit my zoom operation for some area. This zoom in and zoom out is an unlimited one. Do you have any suggestions to do this kind of limitation?

    I appreciate your valuable support for me again my friend.

    Thank You!
     
  6. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,438
    nice!

    you could take distance to that target point, something like:
    Code (CSharp):
    1. Vector3 dist = Vector3.Distance(transform.position, point)
    2.  
    3. // then inside your uparrow and downarrow if's:
    4. if (dist>minDist) { // allow moving forward }
    5.  
    6. if (dist<maxDist) { // then allow moving backwards }
     
    Aravindadx likes this.
  7. Aravindadx

    Aravindadx

    Joined:
    Jul 29, 2018
    Posts:
    14
    Hello @mgear

    Your solution worked perfectly as I need. But somehow there is a little mistake again and I have resolved it. I use float dist instead of Vector3 dist. That means your line number 1 had an error, but I cleared it with following line.

    float dist = Vector3.Distance(transform.position, point)

    So, my final script is below.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityScript;
    5.  
    6. public class CameraMovement : MonoBehaviour {
    7.  
    8.     public float zoomspeed = 5.0f;
    9.     public GameObject targetobject;
    10.     public float rotatespeed = 10.0f;
    11.     private Vector3 point;
    12.  
    13.     void Start()
    14.     {
    15.         point = targetobject.transform.position;
    16.         transform.LookAt(point);
    17.     }
    18.  
    19.     void Update () {
    20.         //Rotate the Entire Camera around the relevant Object
    21.         transform.RotateAround(point, new Vector3(0.0f, -1.0f, 0.0f), 20 * Time.deltaTime * rotatespeed);
    22.  
    23.         //Find the distance between starting and end points when zoom the camera
    24.         float dist = Vector3.Distance(transform.position, point);
    25.  
    26.  
    27.         //if (Input.GetKey(KeyCode.RightArrow))
    28.         //{
    29.         //    transform.position += transform.right * zoomspeed * Time.deltaTime;
    30.         //}
    31.  
    32.         //if (Input.GetKey(KeyCode.LeftArrow))
    33.  
    34.         //{
    35.         //    transform.position -= transform.right * zoomspeed * Time.deltaTime;
    36.  
    37.         //}
    38.  
    39.         //Zoom in and Zoom Out
    40.         if (Input.GetKey(KeyCode.UpArrow))
    41.         {
    42.             if (dist > 1.5)
    43.             {
    44.                 transform.position += transform.forward * zoomspeed * Time.deltaTime;
    45.                 //Debug.Log(dist);
    46.             }
    47.      
    48.         }
    49.  
    50.         if (Input.GetKey(KeyCode.DownArrow))
    51.         {
    52.             if (dist < 7)
    53.             {
    54.                 transform.position -= transform.forward * zoomspeed * Time.deltaTime;
    55.                 //Debug.Log(dist);
    56.             }
    57.         }
    58.     }
    59. }
    I don't need to move my camera to right and left sides. So I commented those lines. Now anyone who have interest about this topic can use the above C# script into unity.

    Problem is solved 100% @mgear . I appreciate your valuable response for me again and again. See you again with a new thread.

    Thank You!
     
    nightowls94s and mgear like this.