Forum rules - please read before posting.

Check Distance from Hotspot

I'm trying to set it up so that if the player is too far from a hotspot they will say "I need to get closer". I know there's the ray length for detecting within a certain range, but I'd love the player to know something is around without having to be close to it.

I can do this in code, but was wondering if there is a feature in Adventure Creator that will allow this already?

Thanks!

Comments

  • What's your Hotspot detection method? If set to Player Vicinity, you can opt to place distance Hotspots on a separate layer (DistantHotspot), which you could raycast for in a custom script to handle a default interaction.

    Otherwise, you could make the check in a custom Action:

    using UnityEngine;
    using System.Collections.Generic;
    
    #if UNITY_EDITOR
    using UnityEditor;
    #endif
    
    namespace AC
    {
    
        [System.Serializable]
        public class ActionRename : ActionCheck
        {
    
            public int constantID = 0;
            public int parameterID = -1;
            public GameObject gameObject;
            protected GameObject runtimeGameObject;
    
            public float maxDistance = 10f;
    
    
            public override ActionCategory Category { get { return ActionCategory.Player; }}
            public override string Title { get { return "Check distance"; }}
            public override string Description { get { return "Checks the distance of an object to the Player."; }}
    
    
            public override void AssignValues (List<ActionParameter> parameters)
            {
                runtimeGameObject = AssignFile (parameters, parameterID, constantID, gameObject);
            }
    
    
            public override float Run ()
            {
                return 0f;
            }
    
    
            public override bool CheckCondition()
            {
                if (runtimeGameObject && KickStarter.player)
                {
                    float distance = Vector3.Distance (runtimeGameObject.gameObject.transform.position, KickStarter.player.transform.position);
                    return distance <= maxDistance;
                }
                return false;
            }
    
    
            #if UNITY_EDITOR
    
            public override void ShowGUI (List<ActionParameter> parameters)
            {
                parameterID = Action.ChooseParameterGUI ("GameObject:", parameters, parameterID, ParameterType.GameObject);
                if (parameterID >= 0)
                {
                    constantID = 0;
                    gameObject = null;
                }
                else
                {
                    gameObject = (GameObject) EditorGUILayout.ObjectField ("GameObject:", gameObject, typeof (GameObject), true);
                    constantID = FieldToID (gameObject, constantID);
                    gameObject = IDToField (gameObject, constantID, false);
                }
    
                maxDistance = EditorGUILayout.FloatField ("Max distance:", maxDistance);
            }
    
            #endif
    
        }
    
    }
    

    Start the Interaction with this, and then have it either give the "too distant" response, or the regular reaction depending on its output.

    This check can be moved to a generic ActionList and parameterised to avoid repeating for each Hotspot interaction, but see if this approach works for you first.

  • Thanks for that, Chris! I'll give it a go and let you know how it turns out.

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.