Forum rules - please read before posting.

Different Ranges for Viewing vs Interaction?

Hi everyone,

I have an interaction question:

Right now, my player can see objects from far away, which is working fine. However, I’d like interactions like use, inspect, etc. to only be available when the player is standing close to the object.

From what I can tell, the interaction range is a global setting. Ideally, I’d like to separate that into two ranges—one for “seeing” and another for all other interactions.

I know I can use triggers to control this, but that approach feels a bit clumsy to manage across multiple scenes.

Any thoughts or suggestions on a cleaner way to handle this?

Thanks!
Tony

Comments

  • Is this for a first-person game, and by "viewing", you're referring to the Hotspot icon / label showing?

    What would you be doing with Triggers that would give the intended result? It may be possible to automate things through script.

  • edited April 8

    It’s a third-person exploration game with kind of a 2D narration layer.

    Right now the player has a short-range scanner—when you get close to a hotspot, you can scan it for extra info. So at the moment, clicking an object gives a “look at” icon, and if you’re within scanner range, you also get a “scan” option.

    What I’m trying to do next is push that same idea into other interactions. Basically, I don’t want the player to be able to use or pick things up unless they’re actually close enough—like within arm’s reach. I can't simply adjust the interaction distance because it would also effect the lookat distance and the player should be able to lookat an object from any distance away.

    I setup the scanner behavior by adding two triggers to an object and use them to detect when the player gets in and out of scanner range.

    One trigger enables the scan interaction (on enter). The other trigger disables the scanner interaction (on exit). It works but I'd really like to have interaction ranges for the other interactions as well. Ideally in the inspector where I could enter a number for each interaction distance.

    I'm not new to scripting, but I am new to Adventure Creator scripts :)

  • Thanks for the details.

    Yeah, if you want to set the distance on a per-interaction level, you'll definitely neeed a custom script.

    Here's a simple one that lets you do this for all "Use" interactions on the attached Hotspot:

    using UnityEngine;
    using AC;
    
    public class RangedInteractions : MonoBehaviour
    {
    
        public RangedInteraction[] rangedInteractions;
    
        void OnEnable() => EventManager.OnHotspotSelect += OnHotspotSelect;
        void OnDisable() => EventManager.OnHotspotSelect -= OnHotspotSelect;
    
        private void OnHotspotSelect(Hotspot hotspot)
        {
            if (hotspot.gameObject == gameObject)
            {
                float sqrDistance = (KickStarter.player.Transform.position - transform.position).sqrMagnitude;
                foreach (var rangedInteraction in rangedInteractions)
                {
                    rangedInteraction.Update(hotspot, sqrDistance);
                }
            }
        }
    
        public class RangedInteraction
        {
            public int interactionIndex;
            public float range = 10f;
    
            public void Update(Hotspot hotspot, float playerSqrDistance)
            {
                bool isNear = playerSqrDistance < range * range;
                hotspot.useButtons[interactionIndex].isDisabled = !isNear;
            }
        }
    
    }
    

    For more on working with Hotspots through script, see the Manual's "Interaction scripting" chapter.

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.