PC-20230316NUNE\Administrator 2b467e56ad no message
2024-02-20 18:39:12 +08:00

85 lines
2.7 KiB
C#

using UnityEngine;
namespace HPJ.Examples
{
/// <summary>
/// A Example Camera Controller for the example scenes
/// </summary>
public class CameraController : MonoBehaviour
{
public float lookSpeed = 5f;
public float moveSpeed = 5f;
public float sprintSpeedMultiplier = 2f;
public float elevationSpeed = 3f;
public bool LockCurser = true;
public bool UseLockCommands = true;
private float rotationX = 0f;
private float rotationY = 0f;
private void Awake()
{
Application.targetFrameRate = 300;
}
void Start()
{
if (LockCurser)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
rotationX = transform.rotation.eulerAngles.x;
rotationY = transform.parent.rotation.eulerAngles.y;
}
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * lookSpeed;
float mouseY = -Input.GetAxis("Mouse Y") * lookSpeed;
if (!Input.GetKey(KeyCode.LeftControl))
{
rotationX += mouseY;
rotationY += mouseX;
rotationX = Mathf.Clamp(rotationX, -90f, 90f);
transform.localRotation = Quaternion.Euler(rotationX, 0f, 0f);
transform.parent.localRotation = Quaternion.Euler(0f, rotationY, 0f);
}
Vector3 moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical"));
if (moveInput.magnitude > 0f)
{
moveInput = Quaternion.Euler(0f, rotationY, 0f) * moveInput;
moveInput.Normalize();
float speedMultiplier = Input.GetKey(KeyCode.LeftShift) ? sprintSpeedMultiplier : 1f;
transform.parent.position += moveInput * moveSpeed * speedMultiplier * Time.deltaTime;
}
if (Input.GetKey(KeyCode.Q))
{
transform.parent.position += Vector3.up * elevationSpeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.E))
{
transform.parent.position -= Vector3.up * elevationSpeed * Time.deltaTime;
}
if (Input.GetKeyDown(KeyCode.Escape) && UseLockCommands)
{
if (Cursor.lockState == CursorLockMode.None)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
else
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
}
}
}
}