Started building house and refined the interact system

This commit is contained in:
2025-12-02 17:13:58 +00:00
parent b71944ef87
commit 732a2577c2
7783 changed files with 68228 additions and 13482 deletions

View File

@@ -22,72 +22,38 @@ public class PlayerController : MonoBehaviour
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
// Lock cursor so it doesn't leave the window
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
// Register Inputs
// --- INPUT BINDINGS ---
// 1. Movement (WASD)
inputActions.Player.Move.performed += ctx => moveInput = ctx.ReadValue<Vector2>();
inputActions.Player.Move.canceled += ctx => moveInput = Vector2.zero;
// 2. Looking (Mouse)
inputActions.Player.Look.performed += ctx => lookInput = ctx.ReadValue<Vector2>();
inputActions.Player.Look.canceled += ctx => lookInput = Vector2.zero;
// 3. Interaction (The "E" Key)
// This listens strictly for the button press.
// It does NOT trigger on collision.
inputActions.Player.Interact.performed += ctx => TryInteract();
}
void OnEnable()
{
if(inputActions != null) inputActions.Enable();
}
void OnDisable()
{
if(inputActions != null) inputActions.Disable();
}
void OnEnable() => inputActions.Enable();
void OnDisable() => 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();
}
UpdateInteractionUI(); // Only updates the text, does not flip switches
}
void HandleMovement()
@@ -108,19 +74,52 @@ public class PlayerController : MonoBehaviour
transform.Rotate(Vector3.up * mouseX);
}
// --- INTERACTION LOGIC ---
// This checks what we are looking at to update the screen text (e.g. "Turn On")
void UpdateInteractionUI()
{
Ray ray = new Ray(cameraTransform.position, cameraTransform.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, interactionDistance, interactionLayer))
{
IInteractable interactable = hit.collider.GetComponent<IInteractable>();
if (interactable != null && InteractionUI.Instance != null)
{
InteractionUI.Instance.UpdatePrompt(interactable.GetDescription());
}
else if (InteractionUI.Instance != null)
{
InteractionUI.Instance.ClearPrompt();
}
}
else
{
if (InteractionUI.Instance != null) InteractionUI.Instance.ClearPrompt();
}
}
// This is ONLY called when 'E' is pressed.
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
// Fire a raycast forward
if (Physics.Raycast(ray, out hit, interactionDistance, interactionLayer))
{
// Did we hit something with the IInteractable script?
IInteractable interactable = hit.collider.GetComponent<IInteractable>();
if (interactable != null)
{
// If yes, execute the code (Flip the switch)
interactable.Interact();
// Immediately update the UI text so it switches from "Turn Off" to "Turn On" instantly
if (InteractionUI.Instance != null)
InteractionUI.Instance.UpdatePrompt(interactable.GetDescription());
}
}
}