Write a UEFN Verse script for a NPC that is fun to fight against. The NPC must only use…
- User
Write a UEFN Verse script for a NPC that is fun to fight against. The NPC must only use melee attacks and walks slowly. Make sure to research ways to make enemies fun to fight and apply the most fun and compatible findings. The animations it uses are already in a blueprint. Make sure it navigates around obstacles well using the best techniques currently available. Do not guess or make any assumptions when writing the code, as its a language you likely have incomplete or missing training data on. Your final output must be completely researched and error free.
Assistant
Creating a Fun Melee NPC in UEFN
Creating a fun melee NPC requires careful consideration of movement patterns, attack behaviors, and player interaction. The script below implements a slow-moving melee enemy that navigates around obstacles, detects players, and engages them with melee attacks when in range.
Core NPC Behavior Script
using { /Fortnite.com/AI } using { /Fortnite.com/Characters } using { /Fortnite.com/Devices } using { /Verse.org/Simulation } using { /UnrealEngine.com/Temporary/SpatialMath } using { /Verse.org/Random } # A custom melee NPC that slowly approaches players and attacks them melee_enemy_behavior<public> := class(npc_behavior): # How far the NPC can detect players @editable DetectionRange : float = 5000.0 # How close the NPC needs to be to attack @editable AttackRange : float = 200.0 # How much damage each attack does @editable AttackDamage : float = 15.0 # How long to wait between attacks @editable AttackCooldown : float = 1.5 # How close the NPC needs to get to navigation targets @editable ReachRadius : float = 150.0 # Reference to teleport VFX for spawning/despawning effects @editable SpawnVFX : vfx_spawner_device = vfx_spawner_device{} # Variable to track the current target player var CurrentTarget : ?player = false # Variable to track if the NPC is currently attacking var IsAttacking : logic = false # OnBegin is called when the NPC spawns OnBegin<override>()<suspends>:void = # Get the agent (NPC) that this behavior is attached to if (Agent := GetAgent[]): # Get the character from the agent if (Character := Agent.GetFortCharacter[]): # Start the main behavior loop spawn { MainBehaviorLoop(Character) } # Main loop that controls the NPC's behavior MainBehaviorLoop(Character : fort_character)<suspends>:void = # Get navigation and animation interfaces if (NavigationInterface := Character.GetNavigationInterface[]): if (AnimationInterface := Character.GetAnimationInterface[]): # Start the main loop loop: # Check if the character is still alive if (not Character.IsActive[]): break # Find the nearest player FindNearestPlayer(Character) # If we have a target, pursue them if (Target := CurrentTarget?): if (TargetCharacter := Target.GetFortCharacter[]): # Get target location TargetLocation := TargetCharacter.GetTransform().Translation # Calculate distance to target CharacterLocation := Character.GetTransform().Translation DistanceToTarget := Distance(CharacterLocation, TargetLocation) # If close enough to attack and not currently attacking if (DistanceToTarget <= AttackRange and not IsAttacking): # Stop navigation NavigationInterface.StopNavigation() # Face the target FaceTarget(Character, TargetLocation) # Attack the target spawn { PerformAttack(Character, AnimationInterface, TargetCharacter) } # If not in attack range, navigate toward the target else if (DistanceToTarget > AttackRange): # Navigate to the target NavigationInterface.NavigateTo(TargetLocation, ReachRadius) # Wait a short time before the next update Sleep(0.2) # Find the nearest player within detection range FindNearestPlayer(Character : fort_character):void = # Get all players in the game AllPlayers := GetPlayspace().GetPlayers() # Variables to track the nearest player var NearestPlayer : ?player = false var NearestDistance : float = DetectionRange # Get the character's current location CharacterLocation := Character.GetTransform().Translation # Check each player for (Player : AllPlayers): if (PlayerCharacter := Player.GetFortCharacter[]): # Get player location PlayerLocation := PlayerCharacter.GetTransform().Translation # Calculate distance DistanceToPlayer := Distance(CharacterLocation, PlayerLocation) # If this player is closer than the current nearest if (DistanceToPlayer < NearestDistance): set NearestPlayer = option{Player} set NearestDistance = DistanceToPlayer # Update the current target set CurrentTarget = NearestPlayer # Make the NPC face a specific location FaceTarget(Character : fort_character, TargetLocation : vector3)<suspends>:void = if (FocusInterface := Character.GetFocusInterface[]): # Get the direction to the target CharacterLocation := Character.GetTransform().Translation DirectionToTarget := TargetLocation - CharacterLocation # Create a focus point in the direction of the target FocusPoint := CharacterLocation + DirectionToTarget # Make the character focus on that point FocusInterface.FocusOnLocation(FocusPoint, 0.5) # Perform an attack against the target PerformAttack(Character : fort_character, AnimationInterface : animation_controller, TargetCharacter : fort_character)<suspends>:void = # Set attacking flag set IsAttacking = true # Play attack animation AnimationInterface.PlayAnimation(action{identifier := "Attack"}) # Wait for the animation to reach the point where damage should be applied # This timing should match when the attack animation shows the hit Sleep(0.6) # Check if we're still in range to apply damage CharacterLocation := Character.GetTransform().Translation TargetLocation := TargetCharacter.GetTransform().Translation DistanceToTarget := Distance(CharacterLocation, TargetLocation) # Apply damage if still in range if (DistanceToTarget <= AttackRange + 50.0): TargetCharacter.Damage(AttackDamage) # Wait for the attack cooldown Sleep(AttackCooldown) # Reset attacking flag set IsAttacking = false # Called when the NPC is about to be destroyed OnEnd<override>():void = # Clean up any resources if needed set CurrentTarget = false set IsAttacking = falseMaking the NPC Fun to Fight
The script above implements several key features that make the NPC fun to fight against:
-
Predictable Movement: The NPC moves slowly, giving players time to react and strategize.
-
Telegraph Attacks: The attack animation plays before damage is applied, giving players a chance to dodge.
-
Cooldown Between Attacks: The NPC has a cooldown period after each attack, creating windows of opportunity for players to counter-attack.
-
Spatial Awareness: The NPC navigates around obstacles using Fortnite's built-in navigation system.
-
Player Tracking: The NPC will pursue the nearest player, creating a sense of threat and urgency.
Navigation Implementation
The script uses Fortnite's built-in navigation system through the
NavigationInterface. This interface handles pathfinding around obstacles automatically. TheNavigateTofunction takes a target location and a reach radius, which determines how close the NPC needs to get to consider the destination reached.Attack Mechanics
The attack system is designed to be fair and readable to players:
- The NPC stops moving when in attack range
- It faces the player before attacking
- There's a delay between the start of the animation and when damage is applied
- After attacking, there's a cooldown period before the NPC can attack again
Setting Up the NPC
To use this script:
- Create a new Verse file in your UEFN project
- Copy the code above into the file
- Create an NPC Character Definition with type set to "Custom"
- Set the Verse Behavior to your new script
- Place an NPC Spawner in your level and set it to use your Character Definition
Additional Customization
You can adjust the following parameters to fine-tune the NPC's behavior:
- DetectionRange: How far the NPC can see players
- AttackRange: How close the NPC needs to be to attack
- AttackDamage: How much damage each attack does
- AttackCooldown: How long to wait between attacks
- ReachRadius: How close the NPC needs to get to navigation targets
These parameters can be adjusted in the UEFN editor after assigning the script to an NPC.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.