JojikYT

Interactable

Jan 26th, 2023
2,061
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.16 KB | Source Code | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class Interactable : MonoBehaviour
  7. {
  8.     [Header("Interaction Data")]
  9.     public string interactableName="";
  10.     public float interactionDistance = 2;
  11.     [SerializeField] bool isInteractable = true;
  12.  
  13.     InteractableNameText interactableNameText;
  14.     GameObject interactableNameCanvas;
  15.  
  16.     public virtual void Start()
  17.     {
  18.         interactableNameCanvas = GameObject.FindGameObjectWithTag("Canvas");
  19.         interactableNameText = interactableNameCanvas.GetComponentInChildren<InteractableNameText>();
  20.     }
  21.  
  22.     public void TargetOn()
  23.     {
  24.         interactableNameText.ShowText(this);
  25.         interactableNameText.SetInteractableNamePosition(this);
  26.     }
  27.  
  28.     public void TargetOff()
  29.     {
  30.         interactableNameText.HideText();
  31.     }
  32.  
  33.     public void Interact()
  34.     {
  35.         if (isInteractable) Interaction();
  36.     }
  37.  
  38.     protected virtual void Interaction()
  39.     {
  40.         //print("interact with: " + this.name);
  41.     }
  42.  
  43.     private void OnDrawGizmos()
  44.     {
  45.         Gizmos.color = Color.blue;
  46.         Gizmos.DrawWireSphere(transform.position,interactionDistance);
  47.     }
  48.     private void OnDestroy()
  49.     {
  50.         TargetOff();
  51.     }
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment