Forum rules - please read before posting.

NPC stop follow player if the player is too far away

Hi Chris,
I have the 3d environment and direct movement setting.
I would like to make the NPC to follow the player only if it is near to the player, it will stop if the player is far away.
I try action NPC follow, it seems that min or max value can't achieve what I want to do.
So, should I attach two triggers to the NPC, one for enter, one for exit to trigger either stop or follow if the player within the NPC triggers.
Is it the correct way to achieve this? Thanks for advise.

Comments

  • I try action NPC follow, it seems that min or max value can't achieve what I want to do.

    These fields are used to ensure that the NPC remains within a set distance range from the character they're following - it won't cancel the follow command.

    Attaching a pair of Triggers to the NPC would be one way to do it, but you could do it through scripting as well:

    using UnityEngine;
    using AC;
    
    public class FollowByDistance : MonoBehaviour
    {
    
        public Cutscene followCutscene;
        private NPC npc;
        private float distanceCheck = 10f;
    
        void Awake ()
        {
            npc = GetComponent <NPC>();
        }
    
        void Update ()
        {
            float distance = Vector3.Distance (gameObject.transform.position, KickStarter.player.transform.position);
    
            if (distance > distanceCheck)
            {
                if (npc.GetFollowTarget () != null)
                {
                    // Stop following
                    npc.StopFollowing ();
                }
            }
            else
            {
                if (npc.GetFollowTarget () == null)
                {
                    // Start following
                    followCutscene.Interact ();
                }
            }
        }
    
    }
    

    That would be attached to the NPC, and assigned a Cutscene that has them follow the Player in its Inspector.

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.