Forum rules - please read before posting.

Hotspot2D - only allow interaction if within certain distance of player

Hi

As the title says, I would like to have hotspots (some, at least) that are visible to the player as per the usual rules (raycast settings, which are for visibility, not interaction as I understand it) but which can only be interacted with if they are "within reach" of the player.
I haven't been able to find any settings or forum threads for this particular situation, so I just wanted to check if there's some mechanic for it already that I am overlooking, before spending time making my own.
It's for a 2D sidescrolling game.

Comments

  • Raycast settings are for Hotspot detection and interaction.

    If you want Hotspots to be interactive if within a certain area of the Player, you can set your Settings Manager's "Hotspot detection method" to "Player Vicinity", and define a Hotspot Detector on your Player to act as the area that they must be within.

    See the Manual's "Player-vicinity detection" chapter for details on this.

    Alternatively, you can assign an "Interactive boundary" to a Hotspot", which the Player must be within in order to interact with that Hotspot.

  • Sounds like it's the last option that is interesting for me, as I want the hotspots to be always detectable but only interactable within an area around the player.
    Will look into the boundary solution - thanks for the guidance!

  • A Hotspot's interactivity and detectability are synced. If you're looking to separate them, you'd have to make it always be interactive, and modify the way the Interaction behaves at the point of the player clicking.

    One way to do this would be to unassign the Interaction ActionList from the Hotspot's Inspector, and instead have a custom script that hooks into the OnHotspotInteract custom event to trigger it only if the Player is within a set distance:

    using UnityEngine;
    using AC;
    
    public class HotspotLimitInteraction : MonoBehaviour
    {
    
        public float minDistance = 5f;
        public Interaction replacementInteraction;
    
        private void OnEnable () { EventManager.OnHotspotInteract += OnHotspotInteract; }
        private void OnDisable () { EventManager.OnHotspotInteract -= OnHotspotInteract; }
    
        private void OnHotspotInteract (Hotspot hotspot, AC.Button button)
        {
            if (hotspot.gameObject == gameObject && 
                Vector3.Distance (KickStarter.player.transform.position, hotspot.transform.position) < minDistance)
            {
                replacementInteraction.Interact ();
            }
        }
    
    }
    
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.