Very late I’m creating an inventory system and to move items through it I use an OnPointerEnter and then a movetowards.
However, when I try to identify if the mouse is over one of the items, it returns as if it were on top of them all.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class ItemUI : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
public bool dragging;
public RPGApplication rpgApplication;
public bool onMouseOver = false;
public int itemIndex;
private void Start()
{
rpgApplication = GameObject.FindGameObjectWithTag("Management").GetComponent<RPGApplication>();
}
public void Update()
{
if (Input.GetButtonDown("Fire1") && onMouseOver)
dragging = !dragging;
else if (Input.GetButtonDown("Fire1") && dragging)
dragging = false;
if (dragging)
{
Vector2 nearTile = Vector2.zero;
foreach (Vector2 vec in rpgApplication.rpgView.playerInventory.inventoryTiles)
if (Vector2.Distance(new Vector2(Input.mousePosition.x, Input.mousePosition.y), vec) < Vector2.Distance(new Vector2(Input.mousePosition.x, Input.mousePosition.y), nearTile))
nearTile = vec;
transform.position = Vector2.MoveTowards(transform.position, nearTile, 600 * Time.deltaTime);// new Vector2(Input.mousePosition.x, Input.mousePosition.y);
rpgApplication.rpgView.playerInventory.itemsList[itemIndex].currentTile = nearTile;
}
}
public void OnPointerEnter(PointerEventData eventData)
{
onMouseOver = true;
}
public void OnPointerExit(PointerEventData eventData)
{
onMouseOver = false;
}
}