103 lines
2.5 KiB
C#
103 lines
2.5 KiB
C#
using UnityEngine;
|
|
using UnityEditor;
|
|
|
|
public class Interactable : MonoBehaviour
|
|
{
|
|
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)
|
|
{
|
|
isFocus = true;
|
|
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.
|
|
}
|
|
|
|
public virtual void OnUnhighlight()
|
|
{
|
|
// Remove highlight effect
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|