Last active
April 23, 2023 16:53
-
-
Save kaiware007/e0cc65124acf7008a5f5a6fa081e683d to your computer and use it in GitHub Desktop.
GameViewのカメラを、SceneViewのカメラと同じような操作感で動かせるスクリプト for 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 UnityEngine; | |
[RequireComponent(typeof(Camera))] | |
public class SceneCameraController : MonoBehaviour | |
{ | |
public Vector3 targetPoint; // 注視点 | |
public float rotateSpeed = 10; | |
public float translateSpeed = 1; | |
public float zoomSpeed = 5; | |
// Update is called once per frame | |
void Update() | |
{ | |
float mouseX = Input.GetAxis("Mouse X"); | |
float mouseY = Input.GetAxis("Mouse Y"); | |
float mouseWheelScroll = Input.GetAxis("Mouse ScrollWheel"); | |
bool isControlAndCommand = Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl) || Input.GetKey(KeyCode.LeftCommand) || Input.GetKey(KeyCode.RightCommand); | |
bool isAlt = Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt); | |
// 平行移動 | |
if (Input.GetMouseButton(2) || (isAlt && isControlAndCommand)) | |
{ | |
targetPoint += transform.right * -mouseX * translateSpeed; | |
targetPoint += transform.up * -mouseY * translateSpeed; | |
this.transform.Translate(-mouseX * translateSpeed, -mouseY * translateSpeed, 0); | |
} | |
// 回転 | |
if (Input.GetMouseButton(1)) | |
{ | |
float dist = Vector3.Distance(this.transform.position, targetPoint); | |
this.transform.rotation = Quaternion.AngleAxis(rotateSpeed * -mouseY, transform.right) * transform.rotation; | |
this.transform.rotation = Quaternion.AngleAxis(rotateSpeed * mouseX, Vector3.up) * transform.rotation; | |
targetPoint = this.transform.position + this.transform.forward * dist; | |
} | |
// ズーム | |
if (mouseWheelScroll != 0) | |
{ | |
this.transform.Translate(Vector3.forward * mouseWheelScroll * zoomSpeed); | |
float dist = Vector3.Distance(this.transform.position, targetPoint); | |
if (dist <= 1f) | |
{ | |
targetPoint = this.transform.position + this.transform.forward * 1f; | |
} | |
} | |
// 注視点の周りを回る | |
if (Input.GetMouseButton(0) && !isControlAndCommand && isAlt) | |
{ | |
this.transform.RotateAround(targetPoint, transform.right, -mouseY * rotateSpeed); | |
this.transform.RotateAround(targetPoint, Vector3.up, mouseX * rotateSpeed); | |
} | |
} | |
private void OnDrawGizmos() | |
{ | |
Gizmos.color = Color.red; | |
Gizmos.DrawWireSphere(targetPoint, 0.1f); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment