Creating an engaging melee-focused NPC in Unreal Editor for Fortnite (UEFN) involves implementing a state machine to manage behaviors, ensuring smooth navigation around obstacles, and providing clear visual and audio cues to enhance player interaction. Below is a comprehensive Verse script that defines such an NPC:
// Define the NPC class inheriting from npc_behavior
melee_npc<public> := class(npc_behavior):
// Editable properties for customization
@editable
AttackRange:float = 200.0 // Distance within which the NPC attacks
@editable
DetectionRange:float = 1000.0 // Distance within which the NPC detects the player
@editable
AttackCooldown:float = 2.0 // Time between attacks
@editable
MovementSpeed:float = 150.0 // NPC's movement speed
@editable
NavigationReachRadius:float = 50.0 // Radius to consider navigation target reached
// Internal variables
var LastAttackTime:float = 0.0
var CurrentState:string = "Idle"
// Reference to the player character
var PlayerCharacter:agent = agent{}
// OnBegin function to initialize NPC behavior
OnBegin<override>()<suspends>:void=
// Get the NPC agent
Agent := GetAgent[]
// Get the Fortnite Character interface
Character := Agent.GetFortCharacter[]
// Set movement speed
Character.SetMovementSpeed(MovementSpeed)
// Main behavior loop
loop:
// Update the NPC's state based on player proximity
UpdateState()
// Execute behavior based on the current state
ExecuteStateBehavior()
// Small delay to prevent excessive processing
Sleep(0.1)
// Function to update the NPC's state
UpdateState():void=
// Get the NPC's current position
NPCPosition := Character.GetTransform().Translation
// Find the nearest player
Players := GetPlayers[]
if (Players.Length > 0):
PlayerCharacter = Players[0]
PlayerPosition := PlayerCharacter.GetTransform().Translation
DistanceToPlayer := (PlayerPosition - NPCPosition).Length()
// Determine state based on distance to player
if (DistanceToPlayer <= AttackRange):
CurrentState = "Attack"
else if (DistanceToPlayer <= DetectionRange):
CurrentState = "Chase"
else:
CurrentState = "Idle"
else:
CurrentState = "Idle"
// Function to execute behavior based on the current state
ExecuteStateBehavior()<suspends>:void=
if (CurrentState == "Idle"):
// NPC remains stationary
Sleep(1.0)
else if (CurrentState == "Chase"):
// Navigate towards the player
NavigateToPlayer()
else if (CurrentState == "Attack"):
// Perform attack if cooldown has elapsed
if (Time() - LastAttackTime >= AttackCooldown):
PerformAttack()
LastAttackTime = Time()
// Function to navigate towards the player
NavigateToPlayer()<suspends>:void=
if (PlayerCharacter != agent{}):
PlayerPosition := PlayerCharacter.GetTransform().Translation
NavTarget := MakeNavigationTarget(PlayerPosition)
Navigatable := Character.GetNavigatable[]
NavResult := Navigatable.NavigateTo(NavTarget, ?ReachRadius := NavigationReachRadius)
if (NavResult == navigation_result.Reached):
// Reached the player, transition to attack state
CurrentState = "Attack"
// Function to perform an attack
PerformAttack()<suspends>:void=
// Play attack animation (assumes animation is set up in the blueprint)
Character.PlayAnimation("AttackAnimation")
// Apply damage to the player (implementation depends on game mechanics)
// Example: PlayerCharacter.ApplyDamage(AttackDamage)
// Wait for attack cooldown
Sleep(AttackCooldown)
Key Features of the Script:
-
State Machine Implementation:
- The NPC operates based on three states: "Idle," "Chase," and "Attack."
- Transitions between states are determined by the distance to the player, enhancing predictability and engagement.
-
Navigation and Obstacle Avoidance:
- Utilizes the
NavigateTo function with a specified ReachRadius to move towards the player, ensuring smooth navigation around obstacles.
-
Melee Attack Execution:
- Performs attacks when within
AttackRange and after the AttackCooldown period, providing clear and consistent combat behavior.
-
Customization:
- Editable properties (
@editable) allow for easy adjustment of detection range, attack range, movement speed, and attack cooldown to fine-tune NPC behavior.
Enhancing Player Engagement:
By integrating these elements, the NPC will provide a challenging yet fair melee combat experience, encouraging players to develop strategies and engage more deeply with the game.