ok so i have a raycast that goes from the camera with a range of 2f with a layer mask for items and harvest items, when the raycast hits an item it enables the ui that tells the player they can pick up that item and when the raycast doesnt hit an item then the display is hidden, but instead of just enabling the ui it rapidly disables and enables it making a flickering effect. ive tried using trigger colliders but the effect is the same.
also it behaves the same with both editor and build play. heres the complete code for the raycast script.
i know theres a lot of iffy stuff regarding input, i just did what i needed to make it work for the time being.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ItemHover : MonoBehaviour
{
public float range = 5f;
public GameObject itemUI;
public Text ItemInfo;
public LayerMask mask;
public bool pickUp;
private DefaultInput defInput;
public InventoryObject inventory;
public string itemUIText;
public GroundItem gI;
public HarvestManager hM;
public Camera cam;
void Awake()
{
defInput = new DefaultInput();
defInput.Player.Interact.started += e => Clicked();
defInput.Player.Interact.canceled += e => UnClick();
defInput.Enable();
gI = null;
hM = null;
}
public void Update()
{
if (gI == null && hM == null)
{
CloseItemMessage();
}
else
{
if(gI != null)
{
OpenItemMessage(itemUIText);
PickUpItem(gI.item, gI.amount, gI);
}
else if(hM != null)
{
OpenItemMessage(itemUIText);
HarvestItem(hM.item, hM.amount, hM);
}
}
GetItemFromRay();
}
public void GetItemFromRay()
{
RaycastHit hit;
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, range, mask))
{
var gItem = hit.transform.GetComponent<GroundItem>();
var hItem = hit.transform.GetComponent<HarvestManager>();
if(gItem == null)
{
gI = null;
}
if(hItem == null)
{
hM = null;
}
if(gItem != null && gItem != gI)
{
gI = gItem;
if (gI.amount > 1)
itemUIText = gI.itemName + " (" + gI.amount + ")";
else
itemUIText = gI.itemName;
}
else if (hItem != null && hItem != hM)
{
hM = hItem;
if (hM.amount > 1)
itemUIText = hM.itemName + " (" + hM.amount + ")";
else
itemUIText = hM.itemName;
}
}
else
{
gI = null;
hM = null;
}
}
public void OpenItemMessage(string text)
{
itemUI.SetActive(true);
ItemInfo.text = text;
}
public void CloseItemMessage()
{
itemUI.SetActive(false);
}
public void Clicked()
{
if (pickUp)
return;
pickUp = true;
}
public void UnClick()
{
if (!pickUp)
return;
pickUp = false;
}
public void PickUpItem(ItemObject _item, int _amount, GroundItem gI)
{
if(pickUp)
{
inventory.AddItem(_item, _amount);
gI.PickedUp();
itemUI.SetActive(false);
UnClick();
}
}
public void HarvestItem(ItemObject _item, int _amount, HarvestManager hM)
{
if (pickUp)
{
inventory.AddItem(_item, _amount);
StartCoroutine(hM.Harvest());
itemUI.SetActive(false);
UnClick();
}
}
}