Advertisement
evelynshilosky

SelectionManager - Part 12

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