How to make camera move in a way similar to editor scene?

 // Zoom
        if (Input.mouseScrollDelta == Vector2.down)
            Camera.main.orthographicSize += 5;
        else if (Input.mouseScrollDelta == Vector2.up)
            Camera.main.orthographicSize -= 5;

        if (Camera.main.orthographicSize < 5)
            Camera.main.orthographicSize = 5;
        else if (Camera.main.orthographicSize > 200)
            Camera.main.orthographicSize = 200;
               
        // rotate left/right, up/down
        if (Input.GetMouseButton(1))
        {
            cameraHolder.Rotate(Vector3.up * Input.GetAxisRaw("Mouse X") * cameraRotateSpeed, Space.Self);
            Camera.main.transform.Rotate(Vector3.right * Input.GetAxisRaw("Mouse Y") * cameraRotateSpeed, Space.Self);
        }

        // Move
        if (Input.GetMouseButton(2))
        {
            cameraHolder.position += new Vector3(Input.GetAxisRaw("Mouse X") * cameraMoveSpeed, 0, Input.GetAxisRaw("Mouse Y") * cameraMoveSpeed);
           
        }

The move part is not correct, i don’t know the scene camera movement refer to, basically adding x,z based on the world axis do not work.

// Move,  Z use camera forward project vector, X use camera right
         if (Input.GetMouseButton(2))
         {
             Vector3 projectVector = Vector3.ProjectOnPlane(Camera.main.transform.forward, Vector3.up);
             cameraHolder.Translate(projectVector* Input.GetAxisRaw("Mouse Y") * cameraMoveSpeed, Space.World);
             cameraHolder.Translate(Camera.main.transform.right * Input.GetAxisRaw("Mouse X") * cameraMoveSpeed, Space.World);
         }

Finally, make the camera exactly same as the editor

Something like this will give WASD control relative to camera:

Script should go on camera gameobject.

   void Update()
   {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");

        Vector3 right = transform.right * h * Time.deltaTime;
        Vector3 forward = transform.forward * v * Time.deltaTime;

        transform.Translate(right + forward);
    }

If you’re talking about how you rotate around objects in the Editor, have a look at Transform.RotateAround().

Maybe look at this post, if that’s what you’re looking for?

This thread is a bit old at this point but I came across it trying to find out how to make camera movement that worked like the camera in the editor window. I didn’t find the answers here helpful to me but I did eventually manage to make a script that does it so I wanted to share it here in case someone else comes across it.

using UnityEngine;

public class CameraController : MonoBehaviour
{
    [SerializeField] float speed = 0.5f;
    [SerializeField] float sensitivity = 1.0f;

    Camera cam;
    Vector3 anchorPoint;
    Quaternion anchorRot;

    private void Awake()
    {
        cam = GetComponent<Camera>();
    }
   
    void FixedUpdate()
    {
        Vector3 move = Vector3.zero;
        if(Input.GetKey(KeyCode.W))
            move += Vector3.forward * speed;
        if (Input.GetKey(KeyCode.S))
            move -= Vector3.forward * speed;
        if (Input.GetKey(KeyCode.D))
            move += Vector3.right * speed;
        if (Input.GetKey(KeyCode.A))
            move -= Vector3.right * speed;
        if (Input.GetKey(KeyCode.E))
            move += Vector3.up * speed;
        if (Input.GetKey(KeyCode.Q))
            move -= Vector3.up * speed;
        transform.Translate(move);

        if (Input.GetMouseButtonDown(1))
        {
            anchorPoint = new Vector3(Input.mousePosition.y, -Input.mousePosition.x);
            anchorRot = transform.rotation;
        }
        if (Input.GetMouseButton(1))
        {
            Quaternion rot = anchorRot;
            Vector3 dif = anchorPoint - new Vector3(Input.mousePosition.y, -Input.mousePosition.x);
            rot.eulerAngles += dif * sensitivity;
            transform.rotation = rot;
        }
    }
}

This script attached to my camera object got me what I wanted.

7 Likes

Here’s something to remember: NEVER use GetInputDown/Up in the fixed loop. They won’t be called properly. On top of that, running movement stuff in the fixed loop (unless you have interpolation) will just make the camera jittery if you’re used to playing at 60 fps.

Here’s my modified version with a little more accurate sensitivity and speed. Note that the speed parameters correspond to the speed parameters of the true camera editor.

I might add camera acceleration and easing later.

using UnityEngine;

public class CameraController : MonoBehaviour {
    [SerializeField] float navigationSpeed = 2.4f;
    [SerializeField] float shiftMultiplier = 2f;
    [SerializeField] float sensitivity = 1.0f;

    private Camera cam;
    private Vector3 anchorPoint;
    private Quaternion anchorRot;

    private void Awake () {
        cam = GetComponent<Camera>();
    }

    void Update () {
        if(Input.GetMouseButton(1)) {
            Vector3 move = Vector3.zero;
            float speed = navigationSpeed * (Input.GetKey(KeyCode.LeftShift) ? shiftMultiplier : 1f) * Time.deltaTime * 9.1f;
            if(Input.GetKey(KeyCode.W))
                move += Vector3.forward * speed;
            if(Input.GetKey(KeyCode.S))
                move -= Vector3.forward * speed;
            if(Input.GetKey(KeyCode.D))
                move += Vector3.right * speed;
            if(Input.GetKey(KeyCode.A))
                move -= Vector3.right * speed;
            if(Input.GetKey(KeyCode.E))
                move += Vector3.up * speed;
            if(Input.GetKey(KeyCode.Q))
                move -= Vector3.up * speed;
            transform.Translate(move);
        }

        if(Input.GetMouseButtonDown(1)) {
            anchorPoint = new Vector3(Input.mousePosition.y, -Input.mousePosition.x);
            anchorRot = transform.rotation;
        }
        if(Input.GetMouseButton(1)) {
            Quaternion rot = anchorRot;

            Vector3 dif = anchorPoint - new Vector3(Input.mousePosition.y, -Input.mousePosition.x);
            rot.eulerAngles += dif * sensitivity;
            transform.rotation = rot;
        }
    }
}
2 Likes

I’m New And Want To use This, But I’m Just Wondering…
Do You Need To Use Time.deltaTime For This? :wink:

Yes you do.
If you want a smooth camera, you must have run all your camera translation/rotation as many times as your scene gets redrawn. But if you remove that Time.delta time, your motion will get tied to your framerate, ex: if you spawn tons of objects in your scene and your frame rate cuts from 120 to 60, your editor camera would do the same amount of translation but x0.5 often, meaning you would move x0.5 slower.

Thanks! Good To Know

What does the *9.1f stand for in your speed calculation? Works great by the way.

I forgot to reply, but I have honestly no idea, you could just remove it and multiply your navigation by it.

Thanks for this. I took the liberty of adding mouse panning and mouse zoom (with shift speed modifier) with the middle mouse button. (Just like in the Unity editor :))

using UnityEngine;
public class CameraController : MonoBehaviour {
    [SerializeField] float navigationSpeed = 2.4f;
    [SerializeField] float shiftMultiplier = 2f;
    [SerializeField] float sensitivity = 1.0f;
    [SerializeField] float panSensitivity = 0.5f;
    [SerializeField] float mouseWheelZoomSpeed = 1.0f;
    private Camera cam;
    private Vector3 anchorPoint;
    private Quaternion anchorRot;
 
    private bool isPanning;
    private void Awake () {
        cam = GetComponent<Camera>();
    }
    void Update () {
    
    
        MousePanning();
        if(isPanning)
        {return;}
    
        if(Input.GetMouseButton(1)) {
            Vector3 move = Vector3.zero;
            float speed = navigationSpeed * (Input.GetKey(KeyCode.LeftShift) ? shiftMultiplier : 1f) * Time.deltaTime * 9.1f;
            if(Input.GetKey(KeyCode.W))
                move += Vector3.forward * speed;
            if(Input.GetKey(KeyCode.S))
                move -= Vector3.forward * speed;
            if(Input.GetKey(KeyCode.D))
                move += Vector3.right * speed;
            if(Input.GetKey(KeyCode.A))
                move -= Vector3.right * speed;
            if(Input.GetKey(KeyCode.E))
                move += Vector3.up * speed;
            if(Input.GetKey(KeyCode.Q))
                move -= Vector3.up * speed;
            transform.Translate(move);
        }
        if(Input.GetMouseButtonDown(1)) {
            anchorPoint = new Vector3(Input.mousePosition.y, -Input.mousePosition.x);
            anchorRot = transform.rotation;
        }
    
        if(Input.GetMouseButton(1)) {
            Quaternion rot = anchorRot;
            Vector3 dif = anchorPoint - new Vector3(Input.mousePosition.y, -Input.mousePosition.x);
            rot.eulerAngles += dif * sensitivity;
            transform.rotation = rot;
        }

        MouseWheeling();
    
    }

    //Zoom with mouse wheel
    void MouseWheeling()
    {
        float speed = 10*(mouseWheelZoomSpeed * (Input.GetKey(KeyCode.LeftShift) ? shiftMultiplier : 1f) * Time.deltaTime * 9.1f);
        
        Vector3 pos = transform.position;
        if (Input.GetAxis("Mouse ScrollWheel") < 0)
        {
            pos = pos - (transform.forward*speed);
            transform.position = pos;
        }
        if (Input.GetAxis("Mouse ScrollWheel") > 0)
        {
            pos = pos + (transform.forward*speed);
            transform.position = pos;
        }
    }


    private float pan_x;
    private float pan_y;
    private Vector3 panComplete;
 
    void MousePanning()
    {
    
        pan_x=-Input.GetAxis("Mouse X")*panSensitivity;
        pan_y=-Input.GetAxis("Mouse Y")*panSensitivity;
        panComplete = new Vector3(pan_x,pan_y,0);
    
        if (Input.GetMouseButtonDown(2))
        {
            isPanning=true;
        }
    
        if (Input.GetMouseButtonUp(2))
        {
            isPanning=false;
        }
    
        if(isPanning)
        {
            transform.Translate(panComplete);
        }
    
 
    }

}
4 Likes

Group effort!

1 Like

I was using your script and i couldn’t figure out why when zooming in or out i was going throught layers intead or getting closer to object.
If any one have the same problem
make sure that the camera is on perspective projection mode and not orthographic

If anyone still searching for one, then :

using UnityEngine;

public class CameraMovement : MonoBehaviour
{
     [Header("REFERENCES")]

     [Header("Camera")]
     public Camera MainCamera;
     [Range(0f,50f)]
     public float CameraFocusPointDistance = 25f;

     [Header("SETTINGS")]

     [Space(2)]
     [Header("Speed")]
     [Range(0f,1f)]
     [Tooltip("The Movement Speed of Camera")]
     public float MovementSpeed;
     [Range(0f,1f)]
     [Tooltip("The On-Axis Rotation Speed of Camera")]
     public float RotationSpeed;
     [Range(0f,15f)]
     [Tooltip("The Zoom speed of Camera")]
     public float ZoomSpeed;
     [Range(45f,450f)]
     [Tooltip("The Revolution speed of Camera")]
     public float RevolutionSpeed;

     [Space(2)]
     [Header("Adjusting Values")]
     [Range(0f,1f)]
     [Tooltip("The amount the Movement speed will be affected by Zoom Input")]
     public float ZoomInputAdjust = 1f;


     //Internal Private Variables
     private float ZoomInputIndex = 1f;
     private RaycastHit hit; //For Storing Focus Point
     void Update()
     {
          //Translation
          float translateX = 0f;
          float translateY = 0f;
         
          //Rotation
          float rotationX = 0f;
          float rotationY = 0f;

          float ZoomInput = Input.GetAxis("Mouse ScrollWheel") * ZoomSpeed;
          ZoomInputIndex += ZoomInput * ZoomInputAdjust;

          if(Input.GetMouseButton(0))
          {
               if(Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt))
               {
                    float revY = Input.GetAxis("Mouse Y");
                    float revX = Input.GetAxis("Mouse X");


                    transform.RotateAround(GetFocusPoint(), Vector3.up, revX * RevolutionSpeed * Time.deltaTime);
                    transform.RotateAround(GetFocusPoint(), transform.right, -revY * RevolutionSpeed * Time.deltaTime);
               }
               else
               {
                    translateY = Input.GetAxis("Mouse Y") * (MovementSpeed/ZoomInputIndex);
                    translateX = Input.GetAxis("Mouse X") * (MovementSpeed/ZoomInputIndex);
               }
          }
          else if(Input.GetMouseButton(1))
          {
               rotationY = Input.GetAxis("Mouse Y") * RotationSpeed;
               rotationX = Input.GetAxis("Mouse X") * RotationSpeed;
          }
         
          transform.Translate(-translateX, -translateY, ZoomInput);
          transform.Rotate(0, rotationX, 0, Space.World);
          transform.Rotate(-rotationY, 0, 0 );
     }

     private Vector3 GetFocusPoint()
     {
          if(Physics.Raycast(MainCamera.transform.position,MainCamera.transform.forward,out hit,CameraFocusPointDistance))
          {
               return hit.point;
          }
          else
          {
               return MainCamera.transform.position + MainCamera.transform.forward * CameraFocusPointDistance;
          }
     }

     [ContextMenu("Auto Assign Variables")]
     public void AutoSetup()
     {
          MainCamera = Camera.main;
     }
}

Just paste it, assign the Camera or from the Context Menu click “Auto Assign Variables” and you’re done.
You can change values according to your preferences.

Includes all controls of editor, focus point calculation, left click and drag to pan, scroll to zoom, right click to rotate on axis and left or right alt + left click to revolve around the focus point.
It also changes and reduces movement speed based on the scroll to prevent large displacement of camera on zoomed in objects.