| | | 1 | | using UnityEngine; |
| | | 2 | | #if UNITY_EDITOR |
| | | 3 | | using UnityEditor; |
| | | 4 | | #endif |
| | | 5 | | |
| | | 6 | | public class MouseFollow : MonoBehaviour |
| | | 7 | | { |
| | | 8 | | [Range(0f, 20f)] |
| | 2 | 9 | | public float sensitivity = 10f; |
| | | 10 | | private float pitch; |
| | | 11 | | private float yaw; |
| | | 12 | | private Camera cam; |
| | | 13 | | void Awake() |
| | 1 | 14 | | { |
| | 1 | 15 | | this.cam = Camera.main; |
| | 1 | 16 | | } |
| | | 17 | | |
| | | 18 | | // Taken from https://stackoverflow.com/questions/66248977/camera-follow-player-when-moving-mouse-with-unity-3d |
| | | 19 | | void Update() |
| | 22125 | 20 | | { |
| | | 21 | | // Work only in the editor in play mode as a debug |
| | | 22 | | // Player must hold right click to enable |
| | | 23 | | #if UNITY_EDITOR |
| | 22125 | 24 | | if (EditorApplication.isPlaying && Input.GetMouseButton(1)) |
| | 0 | 25 | | { |
| | 0 | 26 | | Cursor.lockState = CursorLockMode.Locked; |
| | 0 | 27 | | this.HandleMouseInput(); |
| | 0 | 28 | | } |
| | | 29 | | #endif |
| | 22125 | 30 | | } |
| | | 31 | | |
| | | 32 | | void HandleMouseInput() |
| | | 33 | | { |
| | | 34 | | this.yaw += this.sensitivity * Input.GetAxis("Mouse X"); |
| | | 35 | | this.pitch -= this.sensitivity * Input.GetAxis("Mouse Y"); |
| | | 36 | | this.cam.transform.eulerAngles = new Vector3(this.pitch, this.yaw, 0f); |
| | | 37 | | } |
| | | 38 | | } |