Advertisement
evelynshilosky

SelectionManager - Part 9

Jun 3rd, 2024
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.33 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.     public static SelectionManager Instance { get; set; }      
  9.     public bool onTarget;
  10.     public GameObject selectedObject;
  11.     public GameObject interaction_Info_UI;
  12.     Text interaction_text;
  13.     public Image centerDotImage;
  14.     public Image handIcon;
  15.  
  16.     private void Start()
  17.     {
  18.         onTarget = false;
  19.         interaction_text = interaction_Info_UI.GetComponent<Text>();
  20.     }
  21.     private void Awake()
  22.     {
  23.         if(Instance != null && Instance != this)
  24.         {
  25.             Destroy(gameObject);
  26.         }
  27.         else
  28.         {
  29.             Instance = this;
  30.         }
  31.     }
  32.     void Update()
  33.     {
  34.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  35.         RaycastHit hit;
  36.         if (Physics.Raycast(ray, out hit))
  37.         {
  38.             var selectionTransform = hit.transform;
  39.             InteractableObject interactable = selectionTransform.GetComponent<InteractableObject>();
  40.             if (interactable && interactable.playerInRange)
  41.             {
  42.                 onTarget = true;
  43.                 selectedObject = interactable.gameObject;
  44.                 interaction_text.text = interactable.GetItemName();
  45.                 interaction_Info_UI.SetActive(true);
  46.                 if (interactable.CompareTag("pickable"))
  47.                 {
  48.                     centerDotImage.gameObject.SetActive(false);
  49.                     handIcon.gameObject.SetActive(true);
  50.                 }
  51.                 else
  52.                 {
  53.                     handIcon.gameObject.SetActive(false);
  54.                     centerDotImage.gameObject.SetActive(true);
  55.                 }
  56.             }
  57.             else //if there is a hit, but without an interactable script.
  58.             {
  59.                 onTarget = false;
  60.                 interaction_Info_UI.SetActive(false);
  61.                 handIcon.gameObject.SetActive(false);
  62.                 centerDotImage.gameObject.SetActive(true);
  63.             }
  64.         }
  65.         else //if there is no hit.
  66.         {
  67.             onTarget = false;
  68.             interaction_Info_UI.SetActive(false);
  69.             handIcon.gameObject.SetActive(false);
  70.             centerDotImage.gameObject.SetActive(true);
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement