SHOW:
|
|
- or go back to the newest paste.
| 1 | using UnityEngine; | |
| 2 | using System.Collections; | |
| 3 | ||
| 4 | public class pauseFreeCamera : MonoBehaviour {
| |
| 5 | ||
| 6 | float turnSpeed = 2.0f; // Speed of camera turning when mouse moves in along an axis | |
| 7 | float panSpeed = 1.0f; // Speed of the camera when being panned | |
| 8 | float zoomSpeed = 1.0f; // Speed of the camera going back and forth | |
| 9 | ||
| 10 | private Vector3 mouseOrigin; // Position of cursor when mouse dragging starts | |
| 11 | private bool isPanning; // Is the camera being panned? | |
| 12 | private bool isRotating; // Is the camera being rotated? | |
| 13 | private bool isZooming; // Is the camera zooming? | |
| 14 | ||
| 15 | [HideInInspector] | |
| 16 | public bool usingPauseFreeCamera = false; | |
| 17 | [HideInInspector] | |
| 18 | public bool usingSlowMotion = false; | |
| 19 | ||
| 20 | public MonoBehaviour[] cameraScriptToDisable; | |
| 21 | ||
| 22 | float originalTimeScale; | |
| 23 | ||
| 24 | Vector3 originalCameraPos; | |
| 25 | Quaternion originalCameraRotation; | |
| 26 | ||
| 27 | public KeyCode toggleScreenshotModeKey = KeyCode.F10; | |
| 28 | public KeyCode nextFrameKey = KeyCode.F11; | |
| 29 | public KeyCode toggleSlowMotionModeKey = KeyCode.F12; | |
| 30 | ||
| 31 | Transform originalParent; | |
| 32 | ||
| 33 | private void switchCameraMode() | |
| 34 | {
| |
| 35 | if (usingPauseFreeCamera) | |
| 36 | {
| |
| 37 | usingPauseFreeCamera = false; | |
| 38 | Time.timeScale = 1; | |
| 39 | ||
| 40 | foreach (var script in cameraScriptToDisable) | |
| 41 | {
| |
| 42 | script.enabled = true; | |
| 43 | } | |
| 44 | transform.localPosition = originalCameraPos; | |
| 45 | transform.localRotation = originalCameraRotation; | |
| 46 | transform.parent = originalParent; | |
| 47 | } | |
| 48 | else | |
| 49 | {
| |
| 50 | foreach (var script in cameraScriptToDisable) | |
| 51 | {
| |
| 52 | script.enabled = false; | |
| 53 | } | |
| 54 | originalParent = transform.parent; | |
| 55 | transform.parent = null; | |
| 56 | originalCameraRotation = transform.localRotation; | |
| 57 | originalCameraPos = transform.localPosition; | |
| 58 | Time.timeScale = 0; | |
| 59 | usingPauseFreeCamera = true; | |
| 60 | } | |
| 61 | } | |
| 62 | ||
| 63 | IEnumerator AdvanceOneFrame() | |
| 64 | {
| |
| 65 | Time.timeScale = 0.9f; | |
| 66 | yield return null; | |
| 67 | Time.timeScale = 0; | |
| 68 | } | |
| 69 | ||
| 70 | void Update() | |
| 71 | {
| |
| 72 | if (Input.GetKeyDown(nextFrameKey)) | |
| 73 | {
| |
| 74 | StartCoroutine(AdvanceOneFrame()); | |
| 75 | } | |
| 76 | ||
| 77 | if(Input.GetKeyDown(toggleScreenshotModeKey)) {
| |
| 78 | switchCameraMode(); | |
| 79 | } | |
| 80 | ||
| 81 | if (Input.GetKeyDown(KeyCode.F12)) | |
| 82 | {
| |
| 83 | usingSlowMotion = !usingSlowMotion; | |
| 84 | if (usingSlowMotion == false) | |
| 85 | Time.timeScale = 1; | |
| 86 | } | |
| 87 | ||
| 88 | if (usingSlowMotion) | |
| 89 | {
| |
| 90 | if (Input.GetMouseButtonDown(2) || Input.GetMouseButtonDown(1)) | |
| 91 | {
| |
| 92 | usingSlowMotion = false; | |
| 93 | if (!usingPauseFreeCamera) | |
| 94 | switchCameraMode(); | |
| 95 | } | |
| 96 | if (Input.GetAxis("Mouse ScrollWheel") > 0.1f)
| |
| 97 | {
| |
| 98 | Time.timeScale = Mathf.Clamp(Time.timeScale + 0.35f, 0.1f, 1); | |
| 99 | } | |
| 100 | else if (Input.GetAxis("Mouse ScrollWheel") < -0.1f)
| |
| 101 | {
| |
| 102 | Time.timeScale = Mathf.Clamp(Time.timeScale - 0.35f, 0.1f, 1); | |
| 103 | } | |
| 104 | } | |
| 105 | ||
| 106 | if (!usingPauseFreeCamera) | |
| 107 | return; | |
| 108 | ||
| 109 | if (Input.GetAxisRaw("Vertical") != 0)
| |
| 110 | {
| |
| 111 | transform.localPosition = transform.localPosition + (transform.forward * Input.GetAxisRaw("Vertical") * 0.035f);
| |
| 112 | } | |
| 113 | if (Input.GetAxisRaw("Horizontal") != 0)
| |
| 114 | {
| |
| 115 | transform.localPosition = transform.localPosition + (transform.right * Input.GetAxisRaw("Horizontal") * 0.035f);
| |
| 116 | } | |
| 117 | ||
| 118 | if(Input.GetMouseButtonDown(0)) | |
| 119 | {
| |
| 120 | mouseOrigin = Input.mousePosition; | |
| 121 | isRotating = true; | |
| 122 | } | |
| 123 | ||
| 124 | if(Input.GetMouseButtonDown(1)) | |
| 125 | {
| |
| 126 | mouseOrigin = Input.mousePosition; | |
| 127 | isPanning = true; | |
| 128 | } | |
| 129 | ||
| 130 | if(Input.GetMouseButtonDown(2)) | |
| 131 | {
| |
| 132 | mouseOrigin = Input.mousePosition; | |
| 133 | isZooming = true; | |
| 134 | } | |
| 135 | ||
| 136 | if (!Input.GetMouseButton(0)) isRotating=false; | |
| 137 | if (!Input.GetMouseButton(1)) isPanning=false; | |
| 138 | if (!Input.GetMouseButton(2)) isZooming=false; | |
| 139 | ||
| 140 | if (isRotating) | |
| 141 | {
| |
| 142 | Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin); | |
| 143 | transform.RotateAround(transform.localPosition, transform.right, -pos.y * turnSpeed); | |
| 144 | transform.RotateAround(transform.localPosition, Vector3.up, pos.x * turnSpeed); | |
| 145 | } | |
| 146 | ||
| 147 | if (isPanning) | |
| 148 | {
| |
| 149 | Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin); | |
| 150 | Vector3 move = new Vector3(pos.x * panSpeed, pos.y * panSpeed, 0); | |
| 151 | transform.Translate(move, Space.Self); | |
| 152 | } | |
| 153 | ||
| 154 | if (isZooming) | |
| 155 | {
| |
| 156 | Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin); | |
| 157 | ||
| 158 | Vector3 move = pos.y * zoomSpeed * transform.forward; | |
| 159 | transform.Translate(move, Space.World); | |
| 160 | } | |
| 161 | } | |
| 162 | } |