Advertisement
evelynshilosky

InteractableObject - Part 39

Apr 18th, 2024
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.29 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class InteractableObject : MonoBehaviour
  6. {
  7.  
  8.     public bool playerInRange;
  9.    
  10.     public string ItemName;
  11.  
  12.     [SerializeField] float detectionRange = 10f;
  13.  
  14.     public string GetItemName()
  15.     {
  16.        
  17.         return ItemName;
  18.  
  19.     }
  20.  
  21.  
  22.     void Update()
  23.     {
  24.         float distance = Vector3.Distance(PlayerState.Instance.playerBody.transform.position, transform.position);
  25.  
  26.         if (distance < detectionRange)
  27.         {
  28.             playerInRange = true;
  29.         }
  30.         else
  31.         {
  32.             playerInRange = false;
  33.         }
  34.  
  35.  
  36.         if (Input.GetKeyDown(KeyCode.Mouse0) && playerInRange && SelectionManager.Instance.onTarget && SelectionManager.Instance.selectedObject == gameObject) //add item to inventory
  37.         {
  38.  
  39.             //is the inventory is not full
  40.             if (InventorySystem.Instance.CheckSlotsAvailable(1))
  41.             {
  42.  
  43.                 InventorySystem.Instance.AddToInventory(ItemName);
  44.  
  45.                 InventorySystem.Instance.itemsPickedup.Add(gameObject.name);
  46.  
  47.  
  48.                 Destroy(gameObject);
  49.  
  50.             }
  51.             else
  52.             {
  53.                 Debug.Log("Inventory is Full!");
  54.             }
  55.  
  56.  
  57.         }
  58.  
  59.     }
  60.  
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement