RiftMayhem/Assets/Scripts/Interactables/GamepadInteractionController.cs
Pedro Gomes 8db9d99a02 Input updates
- point and click
- WASD + mouse (WIP)
- Gamepad (WIP)
2025-01-16 19:33:19 +00:00

143 lines
3.8 KiB
C#

using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class GamepadInteractionController : MonoBehaviour
{
[Header("Settings")]
[SerializeField] private float interactionRadius = 3f;
[SerializeField] private float highlightRadius = 5f;
[SerializeField] private LayerMask interactableMask;
[Header("UI References")]
[SerializeField] private GameObject interactionPrompt;
[SerializeField] private Image interactionIcon;
[SerializeField] private TMP_Text interactionText;
private List<Interactable> nearbyInteractables = new List<Interactable>();
private Interactable currentTarget;
private PhotonView photonView;
private bool isShowingInteractables;
private void Awake()
{
photonView = GetComponent<PhotonView>();
}
private void Update()
{
if (!photonView.IsMine) return;
// Show interactables when holding Right Trigger
float rightTrigger = Input.GetAxisRaw("RightTrigger");
isShowingInteractables = rightTrigger > 0.5f;
if (isShowingInteractables)
{
UpdateNearbyInteractables();
HighlightClosestInteractable();
}
else
{
ClearHighlights();
}
// Interact with Right Bumper
if (Input.GetButtonDown("RightBumper") && currentTarget != null)
{
InteractWithCurrent();
}
}
private void UpdateNearbyInteractables()
{
nearbyInteractables.Clear();
Collider[] colliders = Physics.OverlapSphere(transform.position, highlightRadius, interactableMask);
foreach (var collider in colliders)
{
if (collider.TryGetComponent<Interactable>(out var interactable))
{
nearbyInteractables.Add(interactable);
}
}
}
private void HighlightClosestInteractable()
{
float closestDistance = float.MaxValue;
Interactable closest = null;
foreach (var interactable in nearbyInteractables)
{
float distance = Vector3.Distance(transform.position, interactable.transform.position);
if (distance < closestDistance)
{
closestDistance = distance;
closest = interactable;
}
}
// Only highlight if within interaction range
if (closest != null && closestDistance <= interactionRadius)
{
SetCurrentTarget(closest);
}
else
{
ClearCurrentTarget();
}
}
private void SetCurrentTarget(Interactable target)
{
if (currentTarget != target)
{
ClearCurrentTarget();
currentTarget = target;
// Show interaction prompt
interactionPrompt.SetActive(true);
interactionText.text = $"Press RB to {target.GetInteractionText()}";
// Highlight the interactable
target.OnHighlight();
}
}
private void ClearCurrentTarget()
{
if (currentTarget != null)
{
currentTarget.OnUnhighlight();
currentTarget = null;
interactionPrompt.SetActive(false);
}
}
private void ClearHighlights()
{
foreach (var interactable in nearbyInteractables)
{
interactable.OnUnhighlight();
}
nearbyInteractables.Clear();
ClearCurrentTarget();
}
private void InteractWithCurrent()
{
if (currentTarget != null)
{
float distance = Vector3.Distance(transform.position, currentTarget.transform.position);
if (distance <= interactionRadius)
{
currentTarget.Interact(true);
}
}
}
}