Unity URP 에서 기본으로 제공되던 Simple Camare Controler 가 있었지만,
어느순간 이 스크립트는 증발해버렸다 (잠수함패치 싫허효,,)
어디서 긁은거 + GPT를 활용하여 Unity Game View 에서 활용가능한 스크립트를 공유하고자 한다.
using UnityEngine;
public class FlyCamera : MonoBehaviour
{
public Transform targetCamera; // 카메라 오브젝트를 할당할 프로퍼티
public float mainSpeed = 1.0f; // regular speed
public float shiftAdd = 250.0f; // multiplied by how long shift is held. Basically running
public float maxShift = 1000.0f; // Maximum speed when holding shift
public float camSens = 0.25f; // How sensitive it is with mouse
public bool invertY = true;
public float scrollWheelSens = 1f;
public float verticalSpeed = 1.0f; // Q와 E키의 Y축 이동 속도
private Vector3 lastMouse = new Vector3(255, 255, 255); // kind of in the middle of the screen, rather than at the top (play)
private float totalRun = 1.0f;
void Update()
{
if (targetCamera == null)
{
Debug.LogWarning("Target camera is not assigned.");
return;
}
Camera cameraComponent = targetCamera.GetComponent<Camera>();
if (cameraComponent == null)
{
Debug.LogWarning("No Camera component found on target.");
return;
}
if (Input.GetMouseButton(1))
{
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
var mouseMoveY = invertY ? -1 * Input.GetAxis("Mouse Y") : Input.GetAxis("Mouse Y");
var mouseMoveX = Input.GetAxis("Mouse X");
var mouseMove = new Vector3(mouseMoveY, mouseMoveX, 0) * camSens;
targetCamera.eulerAngles = targetCamera.eulerAngles + mouseMove;
}
else
{
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
}
// Mouse camera angle done.
// Keyboard commands
Vector3 p = GetBaseInput();
if (p.sqrMagnitude > 0)
{ // only move while a direction key is pressed
if (Input.GetKey(KeyCode.LeftShift))
{
totalRun += Time.deltaTime;
p = p * totalRun * shiftAdd;
p.x = Mathf.Clamp(p.x, -maxShift, maxShift);
p.y = Mathf.Clamp(p.y, -maxShift, maxShift);
p.z = Mathf.Clamp(p.z, -maxShift, maxShift);
}
else
{
totalRun = Mathf.Clamp(totalRun * 0.5f, 1f, 1000f);
p = p * mainSpeed;
}
p = p * Time.deltaTime;
Vector3 newPosition = targetCamera.position;
if (Input.GetKey(KeyCode.Space))
{ // If player wants to move on X and Z axis only
targetCamera.Translate(p);
newPosition.x = targetCamera.position.x;
newPosition.z = targetCamera.position.z;
targetCamera.position = newPosition;
}
else
{
targetCamera.Translate(p);
}
}
// Q and E key for vertical movement
if (Input.GetKey(KeyCode.Q))
{
targetCamera.position += Vector3.down * mainSpeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.E))
{
targetCamera.position += Vector3.up * mainSpeed * Time.deltaTime;
}
var scroll = Input.GetAxis("Mouse ScrollWheel");
mainSpeed += scroll * scrollWheelSens;
// If the camera is orthographic, adjust the orthographic size with W and S keys
if (cameraComponent.orthographic)
{
if (Input.GetKey(KeyCode.W))
{
cameraComponent.orthographicSize -= mainSpeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.S))
{
cameraComponent.orthographicSize += mainSpeed * Time.deltaTime;
}
// Clamp the orthographic size to prevent it from getting too small or negative
cameraComponent.orthographicSize = Mathf.Clamp(cameraComponent.orthographicSize, 0.1f, 100f);
}
}
private Vector3 GetBaseInput()
{ // returns the basic values, if it's 0 then it's not active.
Vector3 p_Velocity = new Vector3();
if (Input.GetKey(KeyCode.W))
{
p_Velocity += new Vector3(0, 0, 1);
}
if (Input.GetKey(KeyCode.S))
{
p_Velocity += new Vector3(0, 0, -1);
}
if (Input.GetKey(KeyCode.A))
{
p_Velocity += new Vector3(-1, 0, 0);
}
if (Input.GetKey(KeyCode.D))
{
p_Velocity += new Vector3(1, 0, 0);
}
return p_Velocity;
}
}
'Unity > Script' 카테고리의 다른 글
[Unity] Frame Checker On Editor (0) | 2024.06.18 |
---|---|
FrameChecker (0) | 2024.01.02 |