The solution in 2024, I believe, is to use a GKBehavior with a GKGoal(toStayOn:maxPredictionTime:)
The following code will keep an agent within the bounds of a scene, where wh is the width/height of a square-shaped scene.
Depending on your use case, you might need to adjust the weight and/or maxPredictionTime.
let agent = GKAgent2D()
let wh: Float = 2000.0
let bounds = [
SIMD2(x: -(wh*0.5), y: -(wh*0.5)),
SIMD2(x: -(wh*0.5), y: (wh*0.5)),
SIMD2(x: (wh*0.5), y: (wh*0.5)),
SIMD2(x: (wh*0.5), y: -(wh*0.5))
]
let keepInBoundsGoal = GKGoal(toStayOn: GKPath(points: bounds, radius: wh*0.5, cyclical: true), maxPredictionTime: 160.0)
let behavior = GKBehavior(goals: [keepInBoundsGoal])
behavior.setWeight(30.0, for: keepInBoundsGoal)
agent.behavior = behavior
Topic:
Graphics & Games
SubTopic:
General
Tags: