Some checks failed
Build project / Build for (StandaloneWindows64, 6000.0.37f1) (push) Waiting to run
Build project / Publish to itch.io (StandaloneLinux64) (push) Blocked by required conditions
Build project / Publish to itch.io (StandaloneWindows64) (push) Blocked by required conditions
Build project / Build for (StandaloneLinux64, 6000.0.37f1) (push) Has been cancelled
36 lines
1.2 KiB
C#
36 lines
1.2 KiB
C#
using UnityEngine;
|
|
|
|
public class Generator : MonoBehaviour
|
|
{
|
|
public string requiredItemName = "GasTank";
|
|
private bool isFueled = false;
|
|
|
|
void Update()
|
|
{
|
|
if (isFueled) return;
|
|
|
|
Inventory inventory = FindObjectOfType<Inventory>();
|
|
if (inventory == null) return;
|
|
|
|
var heldItem = inventory.slots[inventory.currentSlot];
|
|
if (heldItem != null && heldItem.itemName == requiredItemName && Input.GetMouseButtonDown(0))
|
|
{
|
|
int layerMask = ~LayerMask.GetMask("Player"); // ignore "Player" layer (this is very important to not forget to do in other tasks)
|
|
Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
|
|
if (Physics.Raycast(ray, out RaycastHit hit, 3f, layerMask))
|
|
{
|
|
if (hit.transform == transform || hit.transform.IsChildOf(transform))
|
|
{
|
|
inventory.slots[inventory.currentSlot] = null;
|
|
inventory.EquipSlot(inventory.currentSlot);
|
|
|
|
isFueled = true;
|
|
GetComponent<Collider>().enabled = false;
|
|
|
|
Debug.Log("Generator refueled!");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|