-
-
Save fuqunaga/7e7593cbd7117515e7ffe5bb1ef2dd46 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; | |
private bool _lastFocused; | |
private void Start() | |
{ | |
targetPoint = transform.position + Vector3.forward * 10f; | |
} | |
private void Update() | |
{ | |
// シーンビューにフォーカスを合わせてスクロールするとゲームビューには反映されないが | |
// ゲームビューにフォーカスされたときにシーンビュー上でのスクロールがまとめて返される@Unity2022.3.3f1 | |
// そのため、ゲームビューにフォーカスされたときは無視する | |
var focused = Application.isFocused; | |
if (focused && _lastFocused) | |
{ | |
ApplyInput(); | |
} | |
_lastFocused = focused; | |
} | |
private void ApplyInput() | |
{ | |
var mouseX = Input.GetAxis("Mouse X"); | |
var mouseY = Input.GetAxis("Mouse Y"); | |
var mouseWheelScroll = Input.GetAxis("Mouse ScrollWheel"); | |
var isControlAndCommand = Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl) || Input.GetKey(KeyCode.LeftCommand) || Input.GetKey(KeyCode.RightCommand); | |
var isAlt = Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt); | |
var isShift = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift); | |
var trans = transform; | |
// 平行移動 | |
if (Input.GetMouseButton(2) || (isAlt && isControlAndCommand)) | |
{ | |
var move = new Vector3(-mouseX, -mouseY, 0f) * translateSpeed; | |
var moveWorld = trans.TransformVector(move); | |
// World XZ平面平行移動 | |
if (isShift) moveWorld.y = 0f; | |
targetPoint += moveWorld; | |
trans.Translate(moveWorld, Space.World); | |
} | |
// ズーム | |
if (mouseWheelScroll != 0) | |
{ | |
var moveWorld = trans.forward * (mouseWheelScroll * zoomSpeed); | |
// World XZ平面平行移動 | |
if (isShift) moveWorld.y = 0f; | |
trans.Translate(moveWorld, Space.World); | |
var dist = Vector3.Distance(trans.position, targetPoint); | |
if (dist <= 1f) | |
{ | |
targetPoint = trans.position + trans.forward * 1f; | |
} | |
} | |
// 回転 | |
if (Input.GetMouseButton(1)) | |
{ | |
var pos = trans.position; | |
var dist = Vector3.Distance(pos, targetPoint); | |
// trans.rotation = Quaternion.AngleAxis(rotateSpeed * -mouseY, trans.right) * trans.rotation; | |
// trans.rotation = Quaternion.AngleAxis(rotateSpeed * mouseX, Vector3.up) * trans.rotation; | |
trans.Rotate(Vector3.right, rotateSpeed * -mouseY); | |
trans.Rotate(Vector3.up, rotateSpeed * mouseX, Space.World); | |
targetPoint = pos + trans.forward * dist; | |
} | |
// 注視点の周りを回る | |
if (Input.GetMouseButton(0) && !isControlAndCommand && isAlt) | |
{ | |
trans.RotateAround(targetPoint, trans.right, -mouseY * rotateSpeed); | |
trans.RotateAround(targetPoint, Vector3.up, mouseX * rotateSpeed); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment