RiftMayhem/Assets/Scripts/Interactable.cs
Pedro Gomes d75d51a9c4 Gameplay update
- Added multiple input modes:
   - Point and click (as before)
   - WASD + mouse aiming (fully supported) (need QoL for interactables)
   - Gamepad controls (partial support)

- Sprite indexer to avoid gamebreaking issues when serializing sprites
- Movement speed penalty on casting abilities instead of fully stopping agent
2025-01-17 20:16:02 +00:00

107 lines
2.7 KiB
C#

using UnityEngine;
using UnityEditor;
public class Interactable : MonoBehaviour
{
public GameObject highlight;
[Space]
public float radius = 3f;
public bool interactableWithRange = false;
public float rangedRadius = 10f;
public Transform interactionTransform;
protected bool isFocus = false;
protected Transform player;
protected PlayerController playerController;
protected bool hasInteracted = false;
protected float distance;
protected virtual void Awake()
{
gameObject.layer = 10; //interactable layer
}
public virtual void Interact(bool melee)
{
//this method is meant to be overwritten
//Debug.Log("Interacting with: " + transform.name);
}
protected virtual void Update()
{
if (isFocus && !hasInteracted)
{
distance = Vector3.Distance(player.position, interactionTransform.position);
if (distance <= radius)
{
Interact(true);
hasInteracted = true;
}
else if (interactableWithRange)
{
if (distance <= rangedRadius)
{
Interact(false);
hasInteracted = true;
}
}
}
}
public virtual void OnFocused(Transform playerTransform, PlayerController playerController, bool gamePadFocus = false)
{
isFocus = !gamePadFocus;
player = playerTransform;
this.playerController = playerController;
hasInteracted = false;
}
public virtual void OnDeFocus()
{
isFocus = false;
player = null;
hasInteracted = false;
}
// Add these methods to support gamepad interaction
public virtual void OnHighlight()
{
// Add highlight visual effect
// For example, outline shader, floating arrow, etc.
highlight.SetActive(true);
}
public virtual void OnUnhighlight()
{
// Remove highlight effect
highlight.SetActive(false);
}
public virtual string GetInteractionText()
{
// Return the interaction prompt text
// e.g., "Talk", "Loot", "Open", etc.
return "Interact";
}
private void OnDrawGizmosSelected()
{
if (interactionTransform == null)
{
interactionTransform = transform;
}
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(interactionTransform.position, radius);
if (interactableWithRange)
{
Gizmos.color = Color.magenta;
Gizmos.DrawWireSphere(interactionTransform.position, rangedRadius);
}
}
}