Files
OGG/Assets/Scripts/FirstPersonController.cs

111 lines
3.6 KiB
C#

using Unity.VisualScripting;
using UnityEngine;
public class FirstPersonController : MonoBehaviour
{
[Header("Movement Settings")]
public float walkSpeed = 50f;
public float runSpeed = 80f;
public float jumpHeight = 2.5f;
public float gravity = -20f;
[Header("Mouse Look Settings")]
public float mouseSensitivity = 3f;
public float maxLookAngle = 90f;
[Header("References")]
public Camera playerCamera;
// Private variables
private CharacterController controller;
private Inventory inventory;
private Player player;
private Vector3 velocity;
private bool isGrounded;
private float xRotation = 0f;
void Start()
{
Time.timeScale = 1f;
controller = GetComponent<CharacterController>();
if (controller == null)
{
Debug.LogError("FirstPersonController: No CharacterController found!");
return;
}
if (playerCamera == null)
playerCamera = GetComponentInChildren<Camera>();
inventory = GetComponent<Inventory>();
player = GetComponent<Player>();
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
void Update()
{
if (controller == null) return;
isGrounded = controller.isGrounded;
if (isGrounded && velocity.y < 0)
velocity.y = -2f;
float moveX = 0f, moveZ = 0f;
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)) moveZ += 1f;
if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow)) moveZ -= 1f;
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) moveX -= 1f;
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) moveX += 1f;
Vector3 move = transform.right * moveX + transform.forward * moveZ;
if (move.magnitude > 1f) move.Normalize();
bool wantSprint = Input.GetKey(KeyCode.LeftShift) && move.magnitude > 0f;
bool isSprinting = wantSprint && (player == null || player.CanSprint());
if (player != null) player.isSprinting = isSprinting;
float currentSpeed = isSprinting ? runSpeed : walkSpeed;
controller.Move(move * currentSpeed * Time.deltaTime);
if (Input.GetKey(KeyCode.Space) && isGrounded)
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
velocity.y += gravity * Time.deltaTime;
controller.Move(new Vector3(0f, velocity.y, 0f) * Time.deltaTime);
bool inventoryOpen = inventory != null && inventory.IsOpen;
bool dialogueOpen = DialogueManager.Instance != null && DialogueManager.Instance.IsOpen;
if (!inventoryOpen && !dialogueOpen)
HandleMouseLook();
// Freeze movement during dialogue
if (dialogueOpen) return;
if (Input.GetKeyDown(KeyCode.Escape))
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
if (Input.GetMouseButtonDown(0) && Cursor.lockState == CursorLockMode.None && !inventoryOpen)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
void HandleMouseLook()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -maxLookAngle, maxLookAngle);
playerCamera.transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
transform.Rotate(Vector3.up * mouseX);
}
}