Forum rules - please read before posting.

Set the cursor's position to the selected hotspot/inventory button

Hello,
I'm trying to make the software cursor snap to the location of an inventory item when selected, so that when the item is dragged around it doesn't start from a random positon on the screen when using Keyboard and controller input method with direct control over menus. Is it possible?

Comments

  • If you're using the simulated cursor, you can use the SetSimulatedCursorPosition function to snap it to a particular position:

    AC.KickStarter.playerInput.SetSimulatedCursorPosition (newPosition);
    

    To call this at the moment an item is selected, hook into the OnInventorySelect event:

    using AC;
    using UnityEngine;
    
    public class SnapCursorToItem : MonoBehaviour
    {
    
        private void OnEnable () { EventManager.OnInventorySelect_Alt += OnInventorySelect; }
        private void OnDisable () { EventManager.OnInventorySelect_Alt -= OnInventorySelect; }
    
        private void OnInventorySelect (InvCollection invCollection, InvInstance invInstance)
        {
            Menu menu = PlayerMenus.GetMenuWithName ("Inventory");
            MenuInventoryBox inventoryBox = menu.GetElementWithName ("InventoryBox") as MenuInventoryBox;
            int slot = inventoryBox.GetItemSlot (invInstance);
            Rect slotRect = inventoryBox.GetSlotRectRelative (slot);
            Vector2 newPosition = new Vector2 (slotRect.center.x, Screen.height - slotRect.center.y);
            KickStarter.playerInput.SetSimulatedCursorPosition (newPosition);
        }
    
    }
    

    You'll need to rename the menu/element names in the code to match your own Inventory menu.

  • This is exactly what I was looking for! Thanks a lot!

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.