Advertisement
MariuszPoz

SelectionManager script

Oct 28th, 2023 (edited)
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.02 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using TMPro;
  4.  
  5. public class SelectionManager : MonoBehaviour
  6. {
  7.  
  8.     public GameObject interaction_Info_UI;
  9.     TextMeshProUGUI interaction_text;
  10.  
  11.     private void Start()
  12.     {
  13.         interaction_text = interaction_Info_UI.GetComponent<TextMeshProUGUI>();
  14.     }
  15.  
  16.     void Update()
  17.     {
  18.         Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
  19.         RaycastHit hit;
  20.         if (Physics.Raycast(ray, out hit, 5f))
  21.         {
  22.             var selectionTransform = hit.transform;
  23.  
  24.             if (selectionTransform.GetComponent<InteractableObject>())
  25.             {
  26.                 interaction_text.text = selectionTransform.GetComponent<InteractableObject>().GetItemName();
  27.                 interaction_Info_UI.SetActive(true);
  28.             }
  29.             else
  30.             {
  31.                 interaction_Info_UI.SetActive(false);
  32.             }
  33.  
  34.         }
  35.         else
  36.         {
  37.             interaction_Info_UI.SetActive(false);
  38.         }
  39.     }
  40. }
Tags: #Unity #csharp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement