Creating a compelling AI in UEFN using Verse involves several key components: defining the NPC's behavior, implementing movement and navigation, triggering animations, and handling combat. Here's a breakdown of how to approach this, incorporating concepts from the search results to make the melee-only, slow-moving NPC fun to fight against.
Key Concepts for Fun Melee NPC Combat:
- Predictable but Exploitable Patterns: A slow, melee-only NPC shouldn't be a static target. Give it distinct attack patterns that players can learn to anticipate and counter. This encourages skill-based gameplay.
- Clear Wind-ups and Recovery: Melee attacks should have visible wind-up animations before the hit connects and recovery animations afterward. This gives players time to react (dodge, block, or move away) and creates openings for counter-attacks.
- Telegraphed Attacks: The NPC's intent to attack should be clearly communicated through animation or visual/audio cues. This allows players to respond proactively.
- Meaningful Movement: Even if slow, the NPC's movement should be deliberate. It should close distance to attack, maybe try to flank, or reposition after an attack.
- Responsive to Player Actions: While predictable, the NPC could have slight variations in its behavior based on player actions (e.g., if the player consistently dodges right, the NPC might try a different approach).
- State Machines: A finite-state machine is a good way to structure NPC behavior, defining different states like "Idle," "Patrolling," "Chasing," "Attacking," and transitions between them based on conditions[1][2].
Navigation and Movement:
UEFN utilizes a Navigation Mesh (NavMesh) for AI pathfinding[3][4]. The NavMesh defines walkable areas and helps NPCs find paths around obstacles[3]. Verse provides functions like MoveTo to navigate an actor to a specific location[5][6]. To achieve slow movement, you would likely adjust properties related to movement speed within the NPC's character definition or via Verse[7][8].
Animation:
You can call blueprint events from Verse to trigger animations defined in your NPC's blueprint[9]. This allows you to control which animation plays based on the NPC's current state and actions (e.g., playing a walk animation when moving or an attack animation when in range of a player).
Verse Script Structure (Conceptual):
using { /Fortnite.com/Characters }
using { /Fortnite.com/Devices }
using { /UnrealEngine.com/Temporary/SpatialMath }
using { /UnrealEngine.com/Temporary/Diagnostics }
using { /Verse.org/Simulation }
# Assuming you have a blueprint for your NPC with a Character component
# and functions to trigger animations (e.g., PlayAttackAnimation, PlayWalkAnimation).
# You'll need to set up the Verse callable functions in your blueprint.
melee_npc_behavior<public> := class(npc_behavior):
# Reference to the NPC's character component
@editable
FortCharacter:fort_character = fort_character{}
# Editable references to the blueprint functions for animations
@editable
PlayAttackAnimation:task(agent) : void = task(agent){}
@editable
PlayWalkAnimation:task(agent) : void = task(agent){}
# Movement speed parameter
@editable
MovementSpeed:float = 100.0 # Adjust for slow movement
# Attack range
@editable
AttackRange:float = 200.0 # Adjust based on your NPC's size and attack
# Cooldown duration between attacks
@editable
AttackCooldown:float = 2.0
var IsAttacking:logic = false
var AttackCooldownTimer:float = 0.0
# States for the NPC
enum npc_state:
Idle
Patrolling
Chasing
Attacking
var CurrentState:npc_state = Patrolling
OnBegin<override>()<suspends>:void =
loop:
Sleep(0.1) # Process states periodically
HandleState()
HandleState()<suspend>:void =
match CurrentState:
npc_state.Idle:
# Maybe look around or do a brief idle animation
Print("NPC Idle")
Sleep(1.0) # Stay idle for a bit
# Transition to patrolling or chasing if a player is detected
if (IsPlayerNearby()):
CurrentState = npc_state.Chasing
else:
CurrentState = npc_state.Patrolling
npc_state.Patrolling:
# Implement patrol logic (e.g., move to random points or follow a path)
Print("NPC Patrolling")
MoveToRandomPatrolPoint()
# Transition to chasing if a player is detected
if (IsPlayerNearby()):
CurrentState = npc_state.Chasing
npc_state.Chasing:
Print("NPC Chasing")
# Move towards the nearest player
if (TargetPlayer := GetNearestPlayer()):
MoveTo(TargetPlayer.GetTransform().Translation)
# Transition to attacking if in range
if (Vector3.Distance(FortCharacter.GetTransform().Translation, TargetPlayer.GetTransform().Translation) <= AttackRange):
CurrentState = npc_state.Attacking
else:
# If no player found, go back to patrolling
CurrentState = npc_state.Patrolling
npc_state.Attacking:
Print("NPC Attacking")
if (TargetPlayer := GetNearestPlayer()):
# Ensure the NPC is facing the player (optional but good for melee)
FaceTarget(TargetPlayer.GetTransform().Translation)
if (not IsAttacking and AttackCooldownTimer <= 0.0):
# Trigger attack animation via blueprint
spawn:
PlayAttackAnimation(FortCharacter.GetAgent())
IsAttacking = true
AttackCooldownTimer = AttackCooldown
# Stay in attacking state until attack is finished and cooldown is over
if (IsAttacking):
# You'll need a way to know when the blueprint animation is done
# This might involve the blueprint calling back into Verse via an event
# For now, we'll use a simple delay based on the animation duration
Sleep(1.0) # Approximate attack animation duration - replace with actual mechanism
IsAttacking = false
if (AttackCooldownTimer > 0.0):
AttackCooldownTimer -= 0.1 # Decrement cooldown timer
# Transition back to chasing if the player moves out of range
if (Vector3.Distance(FortCharacter.GetTransform().Translation, TargetPlayer.GetTransform().Translation) > AttackRange):
CurrentState = npc_state.Chasing
else:
# If target somehow disappears, go back to patrolling
CurrentState = npc_state.Patrolling
# Placeholder functions - you would implement these based on your game's logic
IsPlayerNearby()<suspend>:logic =
# Logic to detect if a player is within a certain radius
# Use GetAgents or similar functions to find players
false # Replace with actual detection logic
GetNearestPlayer()<suspend>:?agent =
# Logic to find the nearest player agent
# Use GetAgents and calculate distances
option{} # Replace with actual player finding logic
MoveTo(Destination:vector3)<suspend>:void =
# Use the FortCharacter's movement capabilities to move towards the destination
# You may need to interact with the Character component or a dedicated movement component
# Adjust movement speed here or in character settings
Print("Moving to {Destination}")
FortCharacter.MoveTo(Destination, FortCharacter.GetTransform().Rotation, 1.0) # MoveTo takes a duration
FaceTarget(TargetLocation:vector3)<suspend>:void =
# Logic to rotate the NPC to face the target location
Print("Facing {TargetLocation}")
# Calculate rotation needed and apply it to the FortCharacter
MoveToRandomPatrolPoint()<suspend>:void =
# Implement logic to find a random reachable point on the NavMesh
# Use navigation system functions
Print("Moving to random patrol point")
# Example: Find a random reachable point within a radius
if (RandomLocation := GetRandomReachablePointInRadius(FortCharacter.GetTransform().Translation, 1000.0)):
MoveTo(RandomLocation)
Explanation and Further Considerations:
melee_npc_behavior Class: This class inherits from npc_behavior, which is the base class for creating custom NPC AI in Verse[5][1].
@editable Properties: Properties marked with @editable can be set in the UEFN editor, allowing you to configure the NPC's behavior without changing the Verse code. This is where you would link the NPC's fort_character component and the blueprint animation functions[10].
OnBegin(): This function is called when the NPC is spawned or the game starts[5]. The loop and Sleep(0.1) create a basic tick for the NPC's AI to process its state.
HandleState(): This function contains a match expression that executes different logic based on the NPC's CurrentState[1].
- States: The
npc_state enum defines the different behaviors the NPC can be in[1].
IsPlayerNearby() and GetNearestPlayer(): These are placeholder functions. You would need to implement the logic to detect and find players in your game using Verse functions like GetAgents and calculating distances.
MoveTo(): This function uses the FortCharacter.MoveTo() function to navigate the NPC[5]. You would need to ensure your level has a NavMesh Bounds Volume covering the playable area for navigation to work[4]. Adjusting the duration in MoveTo and potentially the NPC's character movement settings will control the slow walking speed[7][8].
FaceTarget(): This is a placeholder for logic to rotate the NPC to face its target.
MoveToRandomPatrolPoint(): This placeholder represents logic for the patrolling state, using functions like GetRandomReachablePointInRadius to find valid destinations on the NavMesh[4].
- Attack Logic: The
Attacking state includes logic for triggering the attack animation via the blueprint and a simple cooldown timer. You'll need a robust way to determine when the attack animation is finished to transition out of the IsAttacking state. This might involve triggering a Verse event from the blueprint animation's end notification[11].
- Blueprint Interaction: You need to expose functions in your NPC's blueprint that can be called from Verse to trigger animations. This involves setting up Verse Callable functions in your blueprint[9].
Important Notes:
- This is a conceptual script and requires integration with your specific NPC blueprint and game setup in UEFN.
- Implementing robust navigation around complex obstacles will rely on the UEFN navigation system and how you configure the NavMesh[3][4].
- Handling animation states and transitions accurately, especially knowing when an animation is complete, is crucial for smooth behavior. This often involves communication between Verse and blueprint[9][11].
- Verse is an evolving language, and the specifics of API calls and best practices may change. Refer to the official UEFN and Verse documentation for the most up-to-date information.
This detailed approach, incorporating the principles of fun AI design and leveraging UEFN's tools for navigation and animation, provides a solid foundation for creating your slow, melee-only NPC. Remember to iterate and test your NPC's behavior to fine-tune the combat experience.