Forked from kaiware007/SceneCameraController.cs
Last active
December 10, 2025 08:09
-
-
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; | |
| using UnityEngine.InputSystem; | |
| [RequireComponent(typeof(Camera))] | |
| public class SceneCameraController : MonoBehaviour | |
| { | |
| public Vector3 targetPoint; // 注視点 | |
| public float rotateSpeed = 0.2f; | |
| public float translateSpeed = 0.1f; | |
| public float zoomSpeed = 1f; | |
| private void Start() | |
| { | |
| targetPoint = transform.position + Vector3.forward * 10f; | |
| } | |
| private void Update() | |
| { | |
| ApplyInput(); | |
| } | |
| private void ApplyInput() | |
| { | |
| var mouse = Mouse.current; | |
| var keyboard = Keyboard.current; | |
| if (mouse == null) return; | |
| var mouseX = mouse.delta.x.ReadValue(); | |
| var mouseY = mouse.delta.y.ReadValue(); | |
| var mouseWheelScroll = mouse.scroll.y.ReadValue(); | |
| var isControlAndCommand = (keyboard != null && (keyboard.leftCtrlKey.isPressed || | |
| keyboard.rightCtrlKey.isPressed || | |
| keyboard.leftMetaKey.isPressed || | |
| keyboard.rightMetaKey.isPressed)); | |
| var isAlt = keyboard != null && (keyboard.leftAltKey.isPressed || keyboard.rightAltKey.isPressed); | |
| var isShift = keyboard != null && (keyboard.leftShiftKey.isPressed || keyboard.rightShiftKey.isPressed); | |
| var trans = transform; | |
| // 平行移動 | |
| if ((mouse.middleButton.isPressed) || (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 (mouse.rightButton.isPressed) | |
| { | |
| var pos = trans.position; | |
| var dist = Vector3.Distance(pos, targetPoint); | |
| trans.Rotate(Vector3.right, rotateSpeed * -mouseY); | |
| trans.Rotate(Vector3.up, rotateSpeed * mouseX, Space.World); | |
| targetPoint = pos + trans.forward * dist; | |
| } | |
| // 注視点の周りを回る | |
| if (mouse.leftButton.isPressed && !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