Forum rules - please read before posting.

Hold click to interact in context sensitive method

I use context sensitive interaction method.
I want to have the ability to
press and hold left mouse button on a hotspot and select it as an inventory item.

Comments

  • edited January 2021

    You would need a custom script that, when attached to the Hotspot, listens for when the Hotspot is selected by the Player and then reacts to input:

    using UnityEngine;
    using AC;
    
    public class HoldToSelectHotspot : MonoBehaviour
    {
    
        public float maxHoldTime = 1f;
        public int inventoryItemID = 0;
    
        private bool canDetectHold;
        private float holdTime;
    
        private void OnEnable ()
        {
            EventManager.OnHotspotSelect += OnSelect;
            EventManager.OnHotspotDeselect += OnDeselect;
        }
    
        private void OnDisable ()
        {
            EventManager.OnHotspotSelect -= OnSelect;
            EventManager.OnHotspotDeselect -= OnDeselect;
        }
    
        private void Update ()
        {
            if (canDetectHold && Input.GetMouseButton (0) && KickStarter.runtimeInventory.SelectedItem == null)
            {
                holdTime += Time.deltaTime;
                if (holdTime > maxHoldTime)
                {
                    holdTime = 0f;
    
                    if (KickStarter.stateHandler.IsInGameplay ())
                    {
                        KickStarter.runtimeInventory.SelectItemByID (inventoryItemID, SelectItemMode.Use, true);
                    }
                }
            }
        }
    
        private void OnSelect (Hotspot hotspot)
        {
            if (hotspot == GetComponent <Hotspot>())
            {
                canDetectHold = true;
                holdTime = 0f;
            }
        }
    
        private void OnDeselect (Hotspot hotspot)
        {
            if (hotspot == GetComponent <Hotspot>())
            {
                canDetectHold = false;
            }
        }
    
    }
    

    See this related thread as well for another example.

  • Thank you very much, Chris

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.