Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using { /Fortnite.com/AI }
- using { /Fortnite.com/Characters }
- using { /Verse.org/Simulation }
- using { /Verse.org/Random }
- using { /UnrealEngine.com/Temporary/SpatialMath }
- using { /UnrealEngine.com/Temporary/Diagnostics }
- using { /Verse.org/Colors }
- my_debug_draw := class(debug_draw_channel) {
- }
- # BaseFloor height
- BaseFloorHeight : float = 0.0 # change this the the y axis of the elevated floor in uefn (should be 0.0 for ground level)
- # First floor height
- FirstFloorHeight : float = 392.0 # change this the the y axis of the elevated floor in uefn (should be 392.0 for first floor)
- new_npc_behavior_basic := class(npc_behavior):
- DebugDraw:debug_draw = debug_draw{Channel := my_debug_draw}
- # How far the NPC can wander from its spawn point (in meters)
- @editable
- WanderRadius : float = 10.0
- # How long to wait between movements (in seconds)
- @editable
- WaitTime : float = 1.0 # 0 is too snappy and looks fake
- OnBegin<override>()<suspends>:void=
- Print("NPC Behavior: Basic Wander Initialized")
- # Draw debug of the wander radius
- DebugDraw.DrawSphere(vector3{}, ?DrawDurationPolicy := debug_draw_duration_policy.FiniteDuration, ?Radius := WanderRadius, ?Color := NamedColors.Blue)
- if:
- Agent := GetAgent[]
- Character := Agent.GetFortCharacter[]
- Navigatable := Character.GetNavigatable[]
- SpawnPosition := Character.GetTransform().Translation
- then:
- if (Character.IsActive[]):
- Print("Character is active")
- loop:
- TargetPosition : vector3 = GenerateRandomPosition(SpawnPosition, WanderRadius)
- # # Pick a random point within the wander radius
- # RandomOffset := vector3{
- # X := GetRandomFloat(-WanderRadius, WanderRadius),
- # Y := GetRandomFloat(-WanderRadius, WanderRadius),
- # Z := 0.0
- # }
- # TargetPosition := SpawnPosition + RandomOffset
- Print("Spawn position: {SpawnPosition}, Target Position: {TargetPosition}")
- # Get players position
- HeadPosition := Character.GetTransform().Translation + vector3{Y := 3.0}
- # Draw debug line from NPC head to target
- # DebugDraw.DrawLine(vector3{Z:=100.0}, vector3{Z:=200.0}, ?DrawDurationPolicy := debug_draw_duration_policy.Persistent)
- DebugDraw.DrawLine(HeadPosition, TargetPosition, ?Thickness:= 3.0, ?Duration := 6.0)
- DebugDraw.DrawPoint(TargetPosition, ?Thickness:= 100.0, ?Duration := 6.0, ?Color := NamedColors.Red)
- # Try to navigate to the random point
- NavResult := Navigatable.NavigateTo(MakeNavigationTarget(TargetPosition))
- if (NavResult = navigation_result.Reached):
- Print("Reached the target position")
- # Wait at the point for a bit
- Sleep(WaitTime)
- else if (NavResult = navigation_result.Unreachable):
- Print("Target position unreachable, trying again")
- # If we can't reach the point, try again
- Sleep(0.1)
- else:
- # If we can't reach the point, try again
- Print("Could not reach target position")
- GenerateRandomPosition(SpawnPosition:vector3, WanderRadius:float):vector3 =
- # Array of floor heights
- FloorHeights : []float = array{BaseFloorHeight, FirstFloorHeight}
- # Pick a random floor index
- RandomFloorIndex := GetRandomInt(0, FloorHeights.Length - 1)
- if (FloorHeight := FloorHeights[RandomFloorIndex]):
- # Generate random offset including floor height
- RandomOffset := vector3{
- X := GetRandomFloat(-WanderRadius, WanderRadius),
- Y := GetRandomFloat(-WanderRadius, WanderRadius),
- Z := FloorHeight
- }
- Print("Generated random position with floor height: {FloorHeight}")
- return SpawnPosition + RandomOffset
- else:
- # Fallback to ground level if something goes wrong
- Print("Error: Invalid floor height, returning spawn position")
- return SpawnPosition
Advertisement
Add Comment
Please, Sign In to add comment