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

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!

Hi,

can you show some snippet of your current script?

for zooming, have you tried moving camera with its transform.forward?

1 Like

Hello @mgear

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

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityScript;

public class cameraRotate : MonoBehaviour {

public GameObject target;//the target object
public float speedMod = 10.0f;//a speed modifier
private Vector3 point;//the coord to the point where the camera looks at

void Start()
{//Set up things on the start method
point = target.transform.position;//get target's coords
transform.LookAt(point);//makes the camera look to it
}

void Update()
{//makes the camera rotate around "point" coords, rotating around its Y axis, 20 degrees per second times the speed modifier
transform.RotateAround(point, new Vector3(0.0f, -1.0f, 0.0f), 20 * Time.deltaTime * speedMod);
}
}

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

using UnityEngine;
using System.Collections;

public class CameraMovement : MonoBehaviour {

private float speed = 2.0f;
private float zoomSpeed = 2.0f;

public float minX = -360.0f;
public float maxX = 360.0f;

public float minY = -45.0f;
public float maxY = 45.0f;

public float sensX = 100.0f;
public float sensY = 100.0f;

float rotationY = 0.0f;
float rotationX = 0.0f;

void Update () {

float scroll = Input.GetAxis("Mouse ScrollWheel");
transform.Translate(0, scroll * zoomSpeed, scroll * zoomSpeed, Space.World);

if (Input.GetKey(KeyCode.RightArrow)){
transform.position += Vector3.right * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.LeftArrow)){
transform.position += Vector3.left * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.UpArrow)){
transform.position += Vector3.forward * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.DownArrow)){
transform.position += Vector3.back * speed * Time.deltaTime;
}

if (Input.GetMouseButton (0)) {
rotationX += Input.GetAxis ("Mouse X") * sensX * Time.deltaTime;
rotationY += Input.GetAxis ("Mouse Y") * sensY * Time.deltaTime;
rotationY = Mathf.Clamp (rotationY, minY, maxY);
transform.localEulerAngles = new Vector3 (-rotationY, rotationX, 0);
}
}
}

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.

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!

can try replacing those directions with camera directions,

if (Input.GetKey(KeyCode.RightArrow)){
transform.position += transform.right * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.LeftArrow)){
transform.position += transform.left * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.UpArrow)){
transform.position += transform.forward * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.DownArrow)){
transform.position += transform.back * speed * Time.deltaTime;
}

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)

1 Like

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.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityScript;

public class CameraMovement : MonoBehaviour {

public float zoomspeed = 5.0f;
public GameObject targetobject;
public float rotatespeed = 10.0f;
private Vector3 point;

void Start()
{
point = targetobject.transform.position;
transform.LookAt(point);
}

void Update () {
//Rotate the Entire Camera around the relevant Object
transform.RotateAround(point, new Vector3(0.0f, -1.0f, 0.0f), 20 * Time.deltaTime * rotatespeed);

//Zoom in and Zoom Out
if (Input.GetKey(KeyCode.RightArrow))
{
transform.position += transform.right * zoomspeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.position -= transform.right * zoomspeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.UpArrow))
{
transform.position += transform.forward * zoomspeed * Time.deltaTime;

}
if (Input.GetKey(KeyCode.DownArrow))
{
transform.position -= transform.forward * zoomspeed * Time.deltaTime;
}
}
}

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!

nice!

you could take distance to that target point, something like:

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

// then inside your uparrow and downarrow if's:
if (dist>minDist) { // allow moving forward }

if (dist<maxDist) { // then allow moving backwards }
1 Like

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.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityScript;

public class CameraMovement : MonoBehaviour {

public float zoomspeed = 5.0f;
public GameObject targetobject;
public float rotatespeed = 10.0f;
private Vector3 point;

void Start()
{
point = targetobject.transform.position;
transform.LookAt(point);
}

void Update () {
//Rotate the Entire Camera around the relevant Object
transform.RotateAround(point, new Vector3(0.0f, -1.0f, 0.0f), 20 * Time.deltaTime * rotatespeed);

//Find the distance between starting and end points when zoom the camera
float dist = Vector3.Distance(transform.position, point);

//if (Input.GetKey(KeyCode.RightArrow))
//{
//    transform.position += transform.right * zoomspeed * Time.deltaTime;
//}

//if (Input.GetKey(KeyCode.LeftArrow))

//{
//    transform.position -= transform.right * zoomspeed * Time.deltaTime;

//}

//Zoom in and Zoom Out
if (Input.GetKey(KeyCode.UpArrow))
{
if (dist > 1.5)
{
transform.position += transform.forward * zoomspeed * Time.deltaTime;
//Debug.Log(dist);
}

}

if (Input.GetKey(KeyCode.DownArrow))
{
if (dist < 7)
{
transform.position -= transform.forward * zoomspeed * Time.deltaTime;
//Debug.Log(dist);
}
}
}
}

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!

2 Likes