Trouble adding inventory item icon to the mouse cursor on item pickup

Hi there! I’m working on an inventory system for my RPG and I’m having a bit of trouble getting the desired effect for my inventory system’s item moving.

So as it stands, now, I’m using IPointerClickHandler to check if an item is clicked in the inventory and then I’m “picking up the item”. which saves it until it’s put down again. So left click picks, up and left click again in an empty slot, puts down the item.

The issue I’m having is I want the mouse cursor to show what item has been picked up. Currently I have a debug window open that shows me the item name, but I want to show the item’s icon at the mouse cursor when an item is being held.

I’m trying to do something like is seen in Terraria or Stardew Valley, here is an example (sorry for the quality):

6113063--665657--Terraria icon.gif

Any ideas on where to start with this?

Yeah just have an Image element at the root canvas level with the item’s sprite follow the mouse position + an offset.

1 Like

First off, thanks a ton for the fast reply!

That was so easy I feel really silly for even asking haha.

I got it working based on your suggestion with the following code:

private void Update()
    {
        if (heldItem != null)
        {
            mouseItemIcon.GetComponent<Image>().sprite = heldItem.Icon;
            mouseItemIcon.SetActive(true);
            mouseItemIcon.transform.position = Input.mousePosition;
        }
        else
        {
            mouseItemIcon.SetActive(false);
        }
    }

I need to clean up a few things, like storing the Image component rather than getting it every time, this was just something I quickly threw together.

One follow-up question though: Is there any way to make something like this where the item won’t lag behind the mouse so much? Whenever I move the mouse quickly, the item takes a small amount of time to catch up. In Terraria, for example, it’s like glued to the cursor.

The only thing I can think of is actually changing the cursor image itself to represent the held item, but then that would mean I would need a different mouse cursor for every item in the game and that seems awful haha.

It’s not a huge deal, mostly I’m just curious. Thanks again!

Does it seem to lag a little less if you put the code in LateUpdate instead of Update?

Terraria might do something like modifying the cursor itself to include the item icon. Instead of having a cursor icon for every icon int he game you would dynamically create a texture that’s the normal mouse texture + the pixels from the item icon. I’m not sure if they do this though.

1 Like

LateUpdate doesn’t make any real difference on the lag seemingly. Changing the mouse cursor dynamically seems like an interesting solution, and one that I wouldn’t know how to do haha

Maybe later on I would look into doing something like that, but for now, this solution is working out fine, thanks a lot for the help!

1 Like