Created
November 18, 2019 11:47
-
-
Save mu-777/4446ea2df55f7f2dd93f49107520a8dd to your computer and use it in GitHub Desktop.
UnityCameraController
This file contains 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; | |
[RequireComponent(typeof(Camera))] | |
public class CameraController : MonoBehaviour | |
{ | |
enum MouseButton | |
{ | |
LEFT = 0, | |
RIGHT = 1, | |
MIDDLE = 2, | |
}; | |
[SerializeField] | |
private GameObject FocusingObject = null; | |
public float DistanceFromFocusingObject = 3.0f; | |
public bool IsMouseControlActive = true; | |
public KeyCode PositionResetKey = KeyCode.Space; | |
private Vector3 FocusingObjInitPosition; | |
private Vector3 ThisInitPosition; | |
private Vector3 motionStartedMousePos; | |
private const float CameraTrackingScale = 0.005f; | |
private const float CameraRotationScale = 0.05f; | |
void Start() | |
{ | |
if (FocusingObject == null) | |
{ | |
FocusingObject = new GameObject("FocusingObject"); | |
FocusingObject.transform.position = Vector3.zero; | |
} | |
this.transform.parent = FocusingObject.transform; | |
var deltaVec = (this.transform.position - FocusingObject.transform.position); | |
deltaVec = (deltaVec == Vector3.zero) ? (new Vector3(1, 1, 1)).normalized : deltaVec.normalized; | |
this.transform.position = FocusingObject.transform.position + DistanceFromFocusingObject * deltaVec; | |
this.transform.LookAt(FocusingObject.transform.position); | |
FocusingObjInitPosition = FocusingObject.transform.position; | |
ThisInitPosition = this.transform.position; | |
} | |
void Update() | |
{ | |
if (IsMouseControlActive) | |
{ | |
UpdateMouseEvent(); | |
} | |
if (Input.GetKeyDown(PositionResetKey)) | |
{ | |
FocusingObject.transform.position = FocusingObjInitPosition; | |
this.transform.position = ThisInitPosition; | |
} | |
this.transform.LookAt(FocusingObject.transform.position); | |
} | |
public void UpdateMouseEvent() | |
{ | |
//checked new input system: https://blogs.unity3d.com/2019/10/14/introducing-the-new-input-system/ | |
float scroll = Input.GetAxis("Mouse ScrollWheel"); | |
if (scroll != 0.0f) | |
{ | |
CameraDolly(scroll); | |
} | |
if(Input.GetMouseButtonDown((int)MouseButton.MIDDLE) || Input.GetMouseButtonDown((int)MouseButton.RIGHT)) | |
{ | |
this.motionStartedMousePos = Input.mousePosition; | |
} | |
if (Input.GetMouseButton((int)MouseButton.MIDDLE)) | |
{ | |
CameraTrack(Input.mousePosition - this.motionStartedMousePos); | |
this.motionStartedMousePos = Input.mousePosition; | |
} | |
if (Input.GetMouseButton((int)MouseButton.RIGHT)) | |
{ | |
CameraOutsideInRotation(Input.mousePosition - this.motionStartedMousePos); | |
this.motionStartedMousePos = Input.mousePosition; | |
} | |
} | |
private void CameraOutsideInRotation(Vector3 delta) | |
{ | |
delta = (new Vector3(delta.y, delta.x, 0.0f)) * CameraRotationScale; | |
FocusingObject.transform.localEulerAngles += delta; | |
} | |
private void CameraTrack(Vector3 delta) | |
{ | |
delta = delta * CameraTrackingScale; | |
FocusingObject.transform.Translate(Vector3.right * -delta.x); | |
FocusingObject.transform.Translate(Vector3.up * -delta.y); | |
} | |
private void CameraDolly(float delta) | |
{ | |
var deltaVec = (this.transform.position - FocusingObject.transform.position); | |
var updatedDeltaVec = deltaVec * (1.0f + delta); | |
if (updatedDeltaVec.magnitude > 0.01) | |
{ | |
this.transform.position = this.FocusingObject.transform.position + updatedDeltaVec; | |
} | |
return; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment