Files

63 lines
1.4 KiB
C#
Raw Permalink Normal View History

2026-06-04 20:14:47 +03:00
using UnityEngine;
public class ResourceNode : MonoBehaviour
{
public ItemData itemData;
public int amount = 1;
public float interactionRange = 2.4f;
public KeyCode interactionKey = KeyCode.E;
private Transform player;
private bool collected;
private void Start()
{
GameObject playerObject = GameObject.FindGameObjectWithTag("Player");
if (playerObject != null)
{
player = playerObject.transform;
}
}
private void Update()
{
if (collected || itemData == null)
{
return;
}
if (player == null)
{
GameObject playerObject = GameObject.FindGameObjectWithTag("Player");
player = playerObject != null ? playerObject.transform : null;
}
if (player == null || Vector3.Distance(player.position, transform.position) > interactionRange)
{
return;
}
if (Input.GetKeyDown(interactionKey))
{
Collect();
}
}
private void Collect()
{
collected = true;
if (Inventory.Instance != null)
{
Inventory.Instance.AddItem(itemData, amount);
}
if (NotificationUI.Instance != null)
{
NotificationUI.Instance.Show($"Собрано:\n{itemData.itemName} x{amount}");
}
Destroy(gameObject);
}
}