728x90
카메라 이동처리 Script
using UnityEngine; [RequireComponent(typeof(Camera))] public class FlyCamera : MonoBehaviour { public float initialSpeed = 10f; public float increaseSpeed = 1.25f; public bool allowMovement = true; public bool allowRotation = true; public KeyCode forwardButton = KeyCode.W; public KeyCode backwardButton = KeyCode.S; public KeyCode rightButton = KeyCode.D; public KeyCode leftButton = KeyCode.A; public float cursorSensitivity = 0.025f; public bool cursorToggleAllowed = true; public KeyCode cursorToggleButton = KeyCode.Escape; private float currentSpeed = 0f; private bool moving = false; private bool togglePressed = false; private void OnEnable() { if (cursorToggleAllowed) { Cursor.lockState = CursorLockMode.Locked; Cursor.visible = false; } } private void Update() { if (allowMovement) { bool lastMoving = moving; Vector3 deltaPosition = Vector3.zero; if (moving) currentSpeed += increaseSpeed * Time.deltaTime; moving = false; CheckMove(forwardButton, ref deltaPosition, transform.forward); CheckMove(backwardButton, ref deltaPosition, -transform.forward); CheckMove(rightButton, ref deltaPosition, transform.right); CheckMove(leftButton, ref deltaPosition, -transform.right); if (moving) { if (moving != lastMoving) currentSpeed = initialSpeed; transform.position += deltaPosition * currentSpeed * Time.deltaTime; } else currentSpeed = 0f; } if (allowRotation) { Vector3 eulerAngles = transform.eulerAngles; eulerAngles.x += -Input.GetAxis("Mouse Y") * 359f * cursorSensitivity; eulerAngles.y += Input.GetAxis("Mouse X") * 359f * cursorSensitivity; transform.eulerAngles = eulerAngles; } if (cursorToggleAllowed) { if (Input.GetKey(cursorToggleButton)) { if (!togglePressed) { togglePressed = true; Cursor.lockState = CursorLockMode.None; Cursor.visible = !Cursor.visible; } } else togglePressed = false; } else { togglePressed = false; Cursor.visible = false; } } private void CheckMove(KeyCode keyCode, ref Vector3 deltaPosition, Vector3 directionVector) { if (Input.GetKey(keyCode)) { moving = true; deltaPosition += directionVector; } } } |
728x90
'Unity' 카테고리의 다른 글
[Unity] Galaxy S21 (Android 11) 에서 AR Camera 동작 시 앱이 죽는 경우 (1) | 2021.10.07 |
---|---|
[Unity] Android device is not responding (0) | 2021.08.19 |
[Unity] Unity 를 원하는 Visual Studio 버전에 연결하기 (0) | 2021.07.15 |
[Unity] Android Back Button 처리하기 (0) | 2021.07.07 |
[Unity] 2d sprite package from package manager (0) | 2021.07.07 |