Move, Zoom and Rotate Camera – Unity3D (C#)


Move, Zoom and Rotate Camera – Unity3D (C#)

Move: It is same as moving character. Just use keyboard buttons and move camera with a float speed variable.

 

Zoom: To zoom with scroll wheel, we need scroll wheel input to zoom in or zoom out.

 
Rotate: I will give an example with mouse button pressed but you can change it with any button or none. Just change or delete if(Input.GetMouseButton(0)).

We need maximum and minimum values for the axes and assign sensitivities.

You can download source code here. Attach “CameraMovement” script to your camera.

©Coffee Break Codes – Move, Zoom and Rotate Camera – Unity3D (C#)


Leave a comment

Your email address will not be published. Required fields are marked *

7 thoughts on “Move, Zoom and Rotate Camera – Unity3D (C#)

      • sunarto ryan

        Thank you very much!!!!

        This tutorial was work in my Unity!
        This is C# and I like it too, it is simple code.
        I have try to find but can’t find any work but simple code. And finally I’ve found here. Thanks alot.!!!

  • Thomas

    How can I get this to work on a mobile device with touch? Can I get some help on adding that to this code. Otherwise, it works perfectly for me. Thanks!

  • valid22

    Look at this code I’ve used.


    using System;
    using UnityEngine;

    public class Camera1 : MonoBehaviour
    {
    public GameObject Obj = null;
    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;

    float MouseX;
    float MouseY;

    void Update()
    {
    var x = Input.GetAxis("Mouse X");
    var y = Input.GetAxis("Mouse Y");
    if (x != MouseX || y != MouseY)
    {
    rotationX += x * sensX * Time.deltaTime;
    rotationY += y * sensY * Time.deltaTime;
    rotationY = Mathf.Clamp(rotationY, minY, maxY);
    MouseX = x;
    MouseY = y;
    Obj.transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
    }
    }
    }

    Where GameObject is your player. So you can use both mouse and key-buttons and play smoothly!

  • antonyhalfalai

    for code (script of move) version of unity 5,5
    void Update () {
    if (Input.GetKey (KeyCode.W)) { // al precionar la tecla.W))
    transform.Translate (new Vector3 (0, 0, 0.1f)); //cambiar posision.trasladar (aun nuevo vector(usando estas codenadas)
    } else {
    if (Input.GetKey (KeyCode.S)) {
    transform.Translate (new Vector3 (0, 0,-0.1f)); //cambiar posision.trasladar (aun nuevo vector(usando estas codenadas)
    }
    }
    if (Input.GetKey (KeyCode.D)) {
    transform.Translate (new Vector3 (0.1f, 0, 0)); //cambiar posision.trasladar (aun nuevo vector(usando estas codenadas)
    } else {
    if (Input.GetKey (KeyCode.A)) {
    transform.Translate (new Vector3 (-0.1f, 0, 0)); //cambiar posision.trasladar (aun nuevo vector(usando estas codenadas)
    }
    }