RealityKit, failed to move Entity towards a certain direction, with MotionSystem update function

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.

Hi there, have you been able to resolve your issue yet? I think there might be some issues with your code. First, I would try removing the line entity.move(...) and instead doing something like entity.transform = newTransform. Second thing is that I think your let s = ... line will cause your entity to move slower and slower as you get closer to the destination point. So that might be why it only moves once. Instead I would try something like

var dist = (walker.destination! - entity.transform.translation)
dist.y = 0
if (length(dist) > 0.01) {
    entity.transform.translation += 0.3 * fixedDelta * normalize(dist)
}
RealityKit, failed to move Entity towards a certain direction, with MotionSystem update function
 
 
Q