Verse Library verse

01 Fragment

Makes an NPC continuously monitor distance to the nearest player and move toward them when within range.

verse-library/npc-character-behavior/01-fragment.verse

using { /Fortnite.com/AI }

# This is our NPC Behavior class.
# It inherits from npc_behavior.
# This means our script has all the AI tools.
FriendlyGuardBehavior <: npc_behavior:

    # This function runs when the NPC spawns.
    OnBegin (): <override> void:
        # We start a loop to check for the player.
        # This loop runs forever until the NPC dies.
        loop:
            # Get the closest player to this NPC.
            # We use GetClosestPlayer to find who is near.
            player := GetClosestPlayer ()
            
            # Check if the player is within 500 units.
            # 500 units is like walking 5 steps.
            if player != None:
                distance := GetDistance (player)
                if distance < 500:
                    # If close, move towards the player.
                    # We use MoveTo to tell the NPC where to go.
                    MoveTo (player)
                    # Wait a tiny bit so it doesn't glitch.
                    Wait (0.1)
            
            # Wait a short time before checking again.
            # This saves battery (CPU power).
            Wait (0.2)

Comments

    Sign in to vote, comment, or suggest an edit. Sign in