Advertisement
evelynshilosky

SelectionManager - Part 7

Jun 2nd, 2024
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.73 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class SelectionManager : MonoBehaviour
  7. {
  8.  
  9.     public static SelectionManager Instance {get; set;}
  10.  
  11.  
  12.     public bool onTarget;
  13.  
  14.     public GameObject selectedObject;
  15.  
  16.  
  17.     public GameObject interaction_Info_UI;
  18.     Text interaction_text;
  19.  
  20.  
  21.     private void Start()
  22.     {
  23.         onTarget = false;
  24.         interaction_text = interaction_Info_UI.GetComponent<Text>();
  25.     }
  26.  
  27.  
  28.     private void Awake()
  29.     {
  30.         if (Instance !=null && Instance != this)
  31.         {
  32.  
  33.             Destroy(gameObject);
  34.  
  35.         }
  36.         else
  37.         {
  38.             Instance = this;
  39.         }
  40.     }
  41.  
  42.  
  43.     void Update()
  44.     {
  45.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  46.         RaycastHit hit;
  47.         if (Physics.Raycast(ray, out hit))
  48.         {
  49.             var selectionTransform = hit.transform;
  50.  
  51.             InteractableObject interactable = selectionTransform.GetComponent<InteractableObject>();
  52.  
  53.  
  54.             if (interactable && interactable.playerInRange)
  55.             {
  56.  
  57.                 onTarget = true;
  58.                 selectedObject = interactable.gameObject;
  59.                 interaction_text.text = interactable.GetItemName();
  60.                 interaction_Info_UI.SetActive(true);
  61.  
  62.  
  63.  
  64.             }
  65.             else // if there is a hit but without an interatable script
  66.             {
  67.                 onTarget = false;
  68.                 interaction_Info_UI.SetActive(false);
  69.            
  70.             }
  71.  
  72.         }
  73.         else // if there is no hit at all.
  74.         {
  75.             onTarget = false;
  76.             interaction_Info_UI.SetActive(false);
  77.         }
  78.  
  79.  
  80.  
  81.     }
  82.  
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement