Initial commit
This commit is contained in:
127
Assets/Scripts/PlayerController.cs
Normal file
127
Assets/Scripts/PlayerController.cs
Normal file
@@ -0,0 +1,127 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
public class PlayerController : MonoBehaviour
|
||||
{
|
||||
[Header("Movement")]
|
||||
public float moveSpeed = 5f;
|
||||
public float mouseSensitivity = 15f;
|
||||
|
||||
[Header("Interaction")]
|
||||
public float interactionDistance = 3f;
|
||||
public LayerMask interactionLayer;
|
||||
|
||||
private CharacterController controller;
|
||||
private Transform cameraTransform;
|
||||
private float verticalRotation = 0f;
|
||||
|
||||
// Reference to the generated C# class
|
||||
private InputSystem_Actions inputActions;
|
||||
private Vector2 moveInput;
|
||||
private Vector2 lookInput;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
// If this line is red, you haven't clicked "Apply" -> "Generate C# Class"
|
||||
// in the Input Actions Inspector yet.
|
||||
inputActions = new InputSystem_Actions();
|
||||
|
||||
controller = GetComponent<CharacterController>();
|
||||
cameraTransform = Camera.main.transform;
|
||||
|
||||
// Lock cursor
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
Cursor.visible = false;
|
||||
|
||||
// Register Inputs
|
||||
inputActions.Player.Move.performed += ctx => moveInput = ctx.ReadValue<Vector2>();
|
||||
inputActions.Player.Move.canceled += ctx => moveInput = Vector2.zero;
|
||||
|
||||
inputActions.Player.Look.performed += ctx => lookInput = ctx.ReadValue<Vector2>();
|
||||
inputActions.Player.Look.canceled += ctx => lookInput = Vector2.zero;
|
||||
|
||||
inputActions.Player.Interact.performed += ctx => TryInteract();
|
||||
}
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
if(inputActions != null) inputActions.Enable();
|
||||
}
|
||||
|
||||
void OnDisable()
|
||||
{
|
||||
if(inputActions != null) inputActions.Disable();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
HandleMovement();
|
||||
HandleLook();
|
||||
HandleInteractionUI();
|
||||
}
|
||||
void HandleInteractionUI()
|
||||
{
|
||||
Ray ray = new Ray(cameraTransform.position, cameraTransform.forward);
|
||||
RaycastHit hit;
|
||||
|
||||
// Check if we are looking at something interactable
|
||||
if (Physics.Raycast(ray, out hit, interactionDistance, interactionLayer))
|
||||
{
|
||||
IInteractable interactable = hit.collider.GetComponent<IInteractable>();
|
||||
|
||||
if (interactable != null)
|
||||
{
|
||||
// Verify InteractionUI exists to avoid errors
|
||||
if (InteractionUI.Instance != null)
|
||||
{
|
||||
InteractionUI.Instance.UpdatePrompt(interactable.GetDescription());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// We hit something, but it's not interactable
|
||||
if (InteractionUI.Instance != null) InteractionUI.Instance.ClearPrompt();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// We aren't looking at anything
|
||||
if (InteractionUI.Instance != null) InteractionUI.Instance.ClearPrompt();
|
||||
}
|
||||
}
|
||||
|
||||
void HandleMovement()
|
||||
{
|
||||
Vector3 move = transform.right * moveInput.x + transform.forward * moveInput.y;
|
||||
controller.Move(move * moveSpeed * Time.deltaTime);
|
||||
}
|
||||
|
||||
void HandleLook()
|
||||
{
|
||||
float mouseX = lookInput.x * mouseSensitivity * Time.deltaTime;
|
||||
float mouseY = lookInput.y * mouseSensitivity * Time.deltaTime;
|
||||
|
||||
verticalRotation -= mouseY;
|
||||
verticalRotation = Mathf.Clamp(verticalRotation, -90f, 90f);
|
||||
|
||||
cameraTransform.localRotation = Quaternion.Euler(verticalRotation, 0f, 0f);
|
||||
transform.Rotate(Vector3.up * mouseX);
|
||||
}
|
||||
|
||||
void TryInteract()
|
||||
{
|
||||
Ray ray = new Ray(cameraTransform.position, cameraTransform.forward);
|
||||
RaycastHit hit;
|
||||
|
||||
// This is likely where the error was.
|
||||
// We use 4 arguments: Ray, HitInfo, Distance, LayerMask
|
||||
if (Physics.Raycast(ray, out hit, interactionDistance, interactionLayer))
|
||||
{
|
||||
IInteractable interactable = hit.collider.GetComponent<IInteractable>();
|
||||
if (interactable != null)
|
||||
{
|
||||
interactable.Interact();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user