Advertisement
evelynshilosky

SelectionManager - Part 4

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