Guest User

new_npc_behavior_basic

a guest
Aug 2nd, 2025
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. using { /Fortnite.com/AI }
  2. using { /Fortnite.com/Characters }
  3. using { /Verse.org/Simulation }
  4. using { /Verse.org/Random }
  5. using { /UnrealEngine.com/Temporary/SpatialMath }
  6. using { /UnrealEngine.com/Temporary/Diagnostics }
  7. using { /Verse.org/Colors }
  8.  
  9. my_debug_draw := class(debug_draw_channel) {
  10. }
  11.  
  12. # BaseFloor height
  13. BaseFloorHeight : float = 0.0 # change this the the y axis of the elevated floor in uefn (should be 0.0 for ground level)
  14. # First floor height
  15. FirstFloorHeight : float = 392.0 # change this the the y axis of the elevated floor in uefn (should be 392.0 for first floor)
  16.  
  17. new_npc_behavior_basic := class(npc_behavior):
  18. DebugDraw:debug_draw = debug_draw{Channel := my_debug_draw}
  19.  
  20. # How far the NPC can wander from its spawn point (in meters)
  21. @editable
  22. WanderRadius : float = 10.0
  23.  
  24. # How long to wait between movements (in seconds)
  25. @editable
  26. WaitTime : float = 1.0 # 0 is too snappy and looks fake
  27.  
  28. OnBegin<override>()<suspends>:void=
  29. Print("NPC Behavior: Basic Wander Initialized")
  30.  
  31. # Draw debug of the wander radius
  32. DebugDraw.DrawSphere(vector3{}, ?DrawDurationPolicy := debug_draw_duration_policy.FiniteDuration, ?Radius := WanderRadius, ?Color := NamedColors.Blue)
  33. if:
  34. Agent := GetAgent[]
  35. Character := Agent.GetFortCharacter[]
  36. Navigatable := Character.GetNavigatable[]
  37. SpawnPosition := Character.GetTransform().Translation
  38. then:
  39. if (Character.IsActive[]):
  40. Print("Character is active")
  41. loop:
  42. TargetPosition : vector3 = GenerateRandomPosition(SpawnPosition, WanderRadius)
  43. # # Pick a random point within the wander radius
  44. # RandomOffset := vector3{
  45. # X := GetRandomFloat(-WanderRadius, WanderRadius),
  46. # Y := GetRandomFloat(-WanderRadius, WanderRadius),
  47. # Z := 0.0
  48. # }
  49.  
  50. # TargetPosition := SpawnPosition + RandomOffset
  51.  
  52. Print("Spawn position: {SpawnPosition}, Target Position: {TargetPosition}")
  53.  
  54. # Get players position
  55. HeadPosition := Character.GetTransform().Translation + vector3{Y := 3.0}
  56. # Draw debug line from NPC head to target
  57. # DebugDraw.DrawLine(vector3{Z:=100.0}, vector3{Z:=200.0}, ?DrawDurationPolicy := debug_draw_duration_policy.Persistent)
  58. DebugDraw.DrawLine(HeadPosition, TargetPosition, ?Thickness:= 3.0, ?Duration := 6.0)
  59. DebugDraw.DrawPoint(TargetPosition, ?Thickness:= 100.0, ?Duration := 6.0, ?Color := NamedColors.Red)
  60.  
  61. # Try to navigate to the random point
  62. NavResult := Navigatable.NavigateTo(MakeNavigationTarget(TargetPosition))
  63. if (NavResult = navigation_result.Reached):
  64. Print("Reached the target position")
  65. # Wait at the point for a bit
  66. Sleep(WaitTime)
  67. else if (NavResult = navigation_result.Unreachable):
  68. Print("Target position unreachable, trying again")
  69. # If we can't reach the point, try again
  70. Sleep(0.1)
  71. else:
  72. # If we can't reach the point, try again
  73. Print("Could not reach target position")
  74.  
  75.  
  76. GenerateRandomPosition(SpawnPosition:vector3, WanderRadius:float):vector3 =
  77. # Array of floor heights
  78. FloorHeights : []float = array{BaseFloorHeight, FirstFloorHeight}
  79.  
  80. # Pick a random floor index
  81. RandomFloorIndex := GetRandomInt(0, FloorHeights.Length - 1)
  82. if (FloorHeight := FloorHeights[RandomFloorIndex]):
  83. # Generate random offset including floor height
  84. RandomOffset := vector3{
  85. X := GetRandomFloat(-WanderRadius, WanderRadius),
  86. Y := GetRandomFloat(-WanderRadius, WanderRadius),
  87. Z := FloorHeight
  88. }
  89. Print("Generated random position with floor height: {FloorHeight}")
  90. return SpawnPosition + RandomOffset
  91. else:
  92. # Fallback to ground level if something goes wrong
  93. Print("Error: Invalid floor height, returning spawn position")
  94. return SpawnPosition
Advertisement
Add Comment
Please, Sign In to add comment