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; } 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); } } }