Last active
May 30, 2024 09:07
-
-
Save EsProgram/0fd35669c28fd13594c8 to your computer and use it in GitHub Desktop.
Unityのカメラ用スクリプト。Sceneビューのようなマウス操作でカメラを移動可能にする。
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 UnityEngine; | |
/// <summary> | |
/// GameビューにてSceneビューのようなカメラの動きをマウス操作によって実現する | |
/// </summary> | |
[RequireComponent(typeof(Camera))] | |
public class SceneViewCamera : MonoBehaviour | |
{ | |
[SerializeField, Range(0.1f, 10f)] | |
private float wheelSpeed = 1f; | |
[SerializeField, Range(0.1f, 10f)] | |
private float moveSpeed = 0.3f; | |
[SerializeField, Range(0.1f, 10f)] | |
private float rotateSpeed = 0.3f; | |
private Vector3 preMousePos; | |
private void Update() | |
{ | |
MouseUpdate(); | |
return; | |
} | |
private void MouseUpdate() | |
{ | |
float scrollWheel = Input.GetAxis("Mouse ScrollWheel"); | |
if(scrollWheel != 0.0f) | |
MouseWheel(scrollWheel); | |
if(Input.GetMouseButtonDown(0) || | |
Input.GetMouseButtonDown(1) || | |
Input.GetMouseButtonDown(2)) | |
preMousePos = Input.mousePosition; | |
MouseDrag(Input.mousePosition); | |
} | |
private void MouseWheel(float delta) | |
{ | |
transform.position += transform.forward * delta * wheelSpeed; | |
return; | |
} | |
private void MouseDrag(Vector3 mousePos) | |
{ | |
Vector3 diff = mousePos - preMousePos; | |
if(diff.magnitude < Vector3.kEpsilon) | |
return; | |
if(Input.GetMouseButton(2)) | |
transform.Translate(-diff * Time.deltaTime * moveSpeed); | |
else if(Input.GetMouseButton(1)) | |
CameraRotate(new Vector2(-diff.y, diff.x) * rotateSpeed); | |
preMousePos = mousePos; | |
} | |
public void CameraRotate(Vector2 angle) | |
{ | |
transform.RotateAround(transform.position, transform.right, angle.x); | |
transform.RotateAround(transform.position, Vector3.up, angle.y); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment