JojikYT

InteractableNameText

Jan 26th, 2023
2,059
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.90 KB | Source Code | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TMPro;
  5.  
  6. public class InteractableNameText : MonoBehaviour
  7. {
  8.     TextMeshProUGUI text;
  9.  
  10.     Transform cameraTransform;
  11.     void Start()
  12.     {
  13.         text = GetComponentInChildren<TextMeshProUGUI>();
  14.         cameraTransform = Camera.main.transform;
  15.         HideText();
  16.     }
  17.     public void ShowText(Interactable interactable)
  18.     {
  19.         if (interactable is PickUpItem)
  20.         {
  21.             text.text = interactable.interactableName+"\n [E] Pick Up";
  22.         }
  23.         else if (interactable is InteractableChest)
  24.         {
  25.             if (((InteractableChest) interactable).isOpen)
  26.             {
  27.                 text.text = interactable.interactableName + "\n [E] Close";
  28.             }
  29.             else
  30.             {
  31.                 text.text = interactable.interactableName + "\n [E] Open";
  32.             }
  33.         }
  34.         else if(interactable is InteractableLoot)
  35.         {
  36.             text.text = interactable.interactableName + "\n [E] Loot";
  37.         }
  38.         else if(interactable is InteractableNPC)
  39.         {
  40.             text.text = interactable.interactableName + "\n [E] Speak";
  41.         }
  42.         else
  43.         {
  44.             text.text = interactable.interactableName;
  45.         }
  46.  
  47.     }
  48.  
  49.     public void HideText()
  50.     {
  51.         text.text = "";
  52.     }
  53.  
  54.     public void SetInteractableNamePosition(Interactable interactable)
  55.     {
  56.         if (interactable.TryGetComponent(out BoxCollider boxCollider))
  57.         {
  58.             transform.position = interactable.transform.position + Vector3.up * boxCollider.bounds.size.y;
  59.             transform.LookAt(2 * transform.position - cameraTransform.position);
  60.         }
  61.         else if(interactable.TryGetComponent(out CapsuleCollider capsCollider))
  62.         {
  63.             transform.position = interactable.transform.position + Vector3.up * capsCollider.height;
  64.             transform.LookAt(2 * transform.position - cameraTransform.position);
  65.         }
  66.         else
  67.         {
  68.             print("Error, no collider found!");
  69.         }
  70.      
  71.  
  72.     }
  73. }
  74.  
Advertisement
Add Comment
Please, Sign In to add comment