I am trying and trying to make my Entity move towards some previously described destination.
The entity moves on a microlevel over and over again but never reaches its destination. DestinationSystem checks with its update function if the Entity is close enough so it can choose a new destination for it.
I kindly ask you for help as I am absolutely out of ideas.
Here is my DestinationSystem's update function. This system will only chose the current destination for the entity and then checks if the entity reached it.
func update(context: SceneUpdateContext) {
let walkers = context.scene.performQuery(Self.query)
walkers.forEach({ entity in
guard var walker = entity.components[DestinationComponent.self] as? DestinationComponent,
let _ = entity.components[MotionComponent.self] as? MotionComponent else { return }
defer {
entity.components[DestinationComponent.self] = walker
}
// set the default destination to 0
var distanceFromDestination: Float = 0
// check if walker component for this specific entity has any destination
if let destination = walker.destination {
// distract distance from it
distanceFromDestination = entity.distance(from: destination)
}
// check if the distance still is. If its tiny, then lets choose another distance
if distanceFromDestination < 0.02 {
var fixedDestination = SIMD3<Float>.spawnPoint(from: entity.transform.translation, radius: 0.7)
fixedDestination.y = Float(0)
// If there is any obstacle on the way to the destination, set that obstacle as the destination so it wont pass it, perhaps through the wall
let obstacles = context.scene.raycast(
from: entity.position,
to: fixedDestination,
query: .nearest,
mask: .sceneUnderstanding,
relativeTo: nil
)
if let nearest = obstacles.first {
fixedDestination = nearest.position
fixedDestination.y = Float(0)
}
walker.destination = fixedDestination
}
})
}
Here is my MotionSystem. Constant variable s calculates current translation to cross and multiplies it by the deltaTime of the context and 0.03 which I choose as a speed.
func update(context: SceneUpdateContext) {
context.scene.performQuery(Self.query).forEach { entity in
let fixedDelta: Float = Float(context.deltaTime)
guard let _ = entity.components[MotionComponent.self] as? MotionComponent,
let walker = entity.components[DestinationComponent.self] as? DestinationComponent
else { return }
var newTransform = entity.transform
let s = (walker.destination! - entity.transform.translation) * 0.03 * fixedDelta
newTransform.translation = s
newTransform.translation.y = Float(0)
entity.move(to: newTransform, relativeTo: entity.parent)
}
}
I believe it should take my Entity frame by frame to the destination point, then choose another one. It never will again.
Unless my math is terribly off?
Thank you for any help.