Free view camera script.

I want to control camera in my game the same way as in Unity scene view. I want to ask if Unity has already provided this camera script or not. Or do I have to code it myself?

I intensely dislike the orbiting approach, so wrote this one that emulates the movement in UnrealEd (where I cut my 3d teeth :))

using UnityEngine;
using System.Collections;

public class CameraControlScript : MonoBehaviour {

    public float sensitivityX = 8F;
    public float sensitivityY = 8F;

    float mHdg = 0F;
    float mPitch = 0F;

    void Start()
    {
        // owt?
    }

    void Update()
    {
        if (!(Input.GetMouseButton(0) || Input.GetMouseButton(1)))
            return;

        float deltaX = Input.GetAxis("Mouse X") * sensitivityX;
        float deltaY = Input.GetAxis("Mouse Y") * sensitivityY;

        if (Input.GetMouseButton(0) && Input.GetMouseButton(1))
        {
            Strafe(deltaX);
            ChangeHeight(deltaY);
        }
        else
        {
            if (Input.GetMouseButton(0))
            {
                MoveForwards(deltaY);
                ChangeHeading(deltaX);
            }
            else if (Input.GetMouseButton(1))
            {
                ChangeHeading(deltaX);
                ChangePitch(-deltaY);
            }
        }
    }

    void MoveForwards(float aVal)
    {
        Vector3 fwd = transform.forward;
        fwd.y = 0;
        fwd.Normalize();
        transform.position += aVal * fwd;
    }

    void Strafe(float aVal)
    {
        transform.position += aVal * transform.right;
    }

    void ChangeHeight(float aVal)
    {
        transform.position += aVal * Vector3.up;
    }

    void ChangeHeading(float aVal)
    {
        mHdg += aVal;
        WrapAngle(ref mHdg);
        transform.localEulerAngles = new Vector3(mPitch, mHdg, 0);
    }

    void ChangePitch(float aVal)
    {
        mPitch += aVal;
        WrapAngle(ref mPitch);
        transform.localEulerAngles = new Vector3(mPitch, mHdg, 0);
    }

    public static void WrapAngle(ref float angle)
    {
        if (angle < -360F)
            angle += 360F;
        if (angle > 360F)
            angle -= 360F;
    }
}

If you take a look through the standard assets (assuming you don't have Pro) you'll find the script "Mouse Orbit" if you put an object into the Target Slot you get basic mouse orbit functionality. However, this is not the same as the Unity Editor camera behavior.

the camera movement Unity uses is based on how most modern 3D applications work. It's not a very hard thing to code though! You could copy the MouseOrbit script and then change it to only orbit the object when ALT & LMB are pressed. Changing the target to a non-object based position could be a bit harder, especially for a beginner.

My advice is to copy the MouseOrbit class, put in the rotation-handling (alt & LMB) and put in a transform (transform.position) when Ctrl and Alt are held (panning, could be done by calculating the difference in mouse position). How you want to maintain focus on an object (for example by clicking on it) is up to you, but could be done with transform.LookAt.

you have to be aware though that all of the Unity camera movements are relative to a certain plane-of-movement. When panning you could just move up and down, but what if the camera is tilted a bit, you would have to move it diagonally up and down, which probably makes this a logarithmic calculation.

I'd posted a script for this to the forum at some point, but can't find it now via search! So i'm adding it here, as its a bit more clear.

A 3ds max styled orbit/zoom/pan type camera. cobbled together from various sources:

http://www.unifycommunity.com/wiki/index.php?title=MouseOrbitZoom

Regards, Dave.

Just to add on to the wonderful solutions offered above, I believe getting some assets from the Scripting/Camera category on the Asset Store might help. Speaking of orbital movement, perhaps the Camera Orbit Tool is the right one. I understood that you are looking for a free solution according to the subject, but probably spending a little might save you even more time.