Created
January 16, 2020 02:09
-
-
Save calderarchinuk/98ce197b92b38458c8e0516219220072 to your computer and use it in GitHub Desktop.
unreal camera controls in unity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEditor; | |
//TODO clamp pitch rotation | |
//TODO y axis rotation is offset forward slightly, not at scene camera position | |
//TODO sometimes doesn't register mouse up/down events. should poll every frame? for mouse button state | |
public class UnrealCameraMovement : EditorWindow | |
{ | |
[MenuItem ("Window/Mouse Camera Movement Window")] | |
static void Init () { | |
UnrealCameraMovement window = (UnrealCameraMovement)EditorWindow.GetWindow (typeof (UnrealCameraMovement)); | |
window.Show(); | |
} | |
void OnFocus() { | |
SceneView.onSceneGUIDelegate -= this.OnSceneGUI; | |
SceneView.onSceneGUIDelegate += this.OnSceneGUI; | |
} | |
void OnDestroy() { | |
SceneView.onSceneGUIDelegate -= this.OnSceneGUI; | |
} | |
bool LeftMouse = false; | |
bool RightMouse = false; | |
Vector2 lastMousePosition; | |
float RotationSpeed = 0.5f; | |
float MoveSpeed = 0.5f; | |
void OnSceneGUI(SceneView sceneView) | |
{ | |
Event e = Event.current; | |
var currentMousePos = e.mousePosition; | |
var startCamPos = sceneView.camera.transform.position; | |
var startCamRot = sceneView.camera.transform.rotation; | |
if (e.button == 1 && e.type == EventType.MouseDown) | |
{ | |
lastMousePosition = currentMousePos; | |
RightMouse = true; | |
//GUIUtility.hotControl = 0; | |
} | |
if (e.button == 1 && e.type == EventType.MouseUp) | |
{ | |
Debug.Log("right mouse up"); | |
RightMouse = false; | |
} | |
if (e.button == 0 && e.type == EventType.MouseDown) | |
{ | |
lastMousePosition = currentMousePos; | |
LeftMouse = true; | |
} | |
if (Event.current.isMouse && Event.current.button == 0 && (Event.current.type == EventType.MouseDown)) | |
{ | |
Debug.Log("click"); | |
GUIUtility.hotControl = 0; | |
} | |
if (e.button == 0 && e.type == EventType.MouseUp) | |
{ | |
Debug.Log("left mouse up"); | |
LeftMouse = false; | |
} | |
delta = (currentMousePos - lastMousePosition) * 1; | |
if (LeftMouse && !RightMouse) | |
{ | |
//y is local forward and backward (x/z axis) | |
//x is rotate z axis | |
//y | |
Vector3 position = SceneView.lastActiveSceneView.pivot; | |
Vector3 direction = SceneView.lastActiveSceneView.camera.transform.forward; | |
direction.y = 0; | |
Vector3 newPos = position + direction * -delta.y * MoveSpeed; | |
SceneView.lastActiveSceneView.pivot = newPos; | |
//x | |
SceneView.lastActiveSceneView.camera.transform.RotateAround( | |
SceneView.lastActiveSceneView.camera.transform.position, | |
Vector3.up, | |
delta.x * RotationSpeed); | |
SceneView.lastActiveSceneView.rotation = SceneView.lastActiveSceneView.camera.transform.rotation; | |
} | |
else if (!LeftMouse && RightMouse) | |
{ | |
//x is rotate left/right z axis | |
//y is rotate up/down local x axis | |
//x | |
SceneView.lastActiveSceneView.camera.transform.RotateAround( | |
SceneView.lastActiveSceneView.camera.transform.position, | |
Vector3.up, | |
delta.x * RotationSpeed); | |
SceneView.lastActiveSceneView.rotation = SceneView.lastActiveSceneView.camera.transform.rotation; | |
//y | |
SceneView.lastActiveSceneView.camera.transform.RotateAround( | |
SceneView.lastActiveSceneView.camera.transform.position, | |
SceneView.lastActiveSceneView.camera.transform.right, | |
delta.y * RotationSpeed); | |
//TODO clamp rotation | |
SceneView.lastActiveSceneView.rotation = SceneView.lastActiveSceneView.camera.transform.rotation; | |
} | |
else if (LeftMouse && RightMouse) | |
{ | |
//x pan left/right local x axis | |
//y pan up/down global z axis | |
//x and y | |
Vector3 position = SceneView.lastActiveSceneView.pivot; | |
Vector3 direction = SceneView.lastActiveSceneView.camera.transform.right; | |
Vector3 newPos = position + direction * delta.x * MoveSpeed + Vector3.up * -delta.y * MoveSpeed; | |
SceneView.lastActiveSceneView.pivot = newPos; | |
} | |
SceneView.lastActiveSceneView.Repaint(); | |
lastMousePosition = currentMousePos; | |
} | |
Vector2 delta; | |
void OnGUI() | |
{ | |
GUILayout.Label("left " + LeftMouse); | |
GUILayout.Label("right " + RightMouse); | |
RotationSpeed = EditorGUILayout.FloatField("rotation",RotationSpeed); | |
MoveSpeed = EditorGUILayout.FloatField("move",MoveSpeed); | |
if (GUILayout.Button("reset")) | |
{ | |
LeftMouse = false; | |
RightMouse = false; | |
} | |
GUILayout.Label(delta.ToString()); | |
Repaint(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment