// 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
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.
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.
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 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 :))
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
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.