我有一个简单的基于SKScene的游戏。我有一个使用GKGridGraph.findPath创建的路径,我希望我的代理完全遵循这个路径。
我已经设置了代理来跟随路径,半径为1,所以它应该是一条很窄的路径,沿着网格的中心。
class EnemyAgent: GKAgent2D {
init(path: GKPath) {
super.init()
maxAcceleration = 1000
maxSpeed = 100
mass = 1.0
radius = 1.0
let followGoal = GKGoal(toFollow: path, maxPredictionTime: 1.0, forward: true)
let stayOnPathGoal = GKGoal(toStayOn: path, maxPredictionTime: 1.0)
behavior = GKBehavior(goals: [stayOnPathGoal, followGoal], andWeights: [50, 100])
}我遇到的问题是,代理似乎有一个转弯半径,可能是基于质量和加速度,这阻止了它粘附在网格的中心。

我能做些什么来迫使实体完全停留在这条道路上?
发布于 2022-06-03 11:04:40
GameplayKit似乎没有办法准确地遵循一条路径,但SpriteKit却这样做了。我能够让节点精确地跟随网格,使用SKAction设置来跟踪CGPath。
func followPath() {
let path = CGMutablePath()
tilePath.forEach { pt in
if path.isEmpty {
path.move(to: pt)
} else {
path.addLine(to: pt)
}
}
let action = SKAction.follow(path, asOffset: false, orientToPath: true, speed: 50.0)
sprite.run(action) { [weak self] in
print("path done!")
self?.sprite.removeFromParent()
}
}这比使用GameplayKit更不灵活,因为路径不会受到不同目标的影响,但是我看不出有任何方法可以获得GameplayKit所遵循的确切路径。
https://stackoverflow.com/questions/72460870
复制相似问题