Home > Enterprise >  RealityKit – move(to:) methods work only without duration parameter
RealityKit – move(to:) methods work only without duration parameter

Time:10-10

Using move(to:) method to update Entity's position works only if I don't use the the initialiser with duration parameter.

sphere.move(to: newTransform, relativeTo: nil, duration: 0.75) // Absolutely no effect
sphere.move(to: newTransform, relativeTo: nil) // Instant effect

Both called from the Main thread. I don't understand what may cause this strange behaviour.

CodePudding user response:

Implement move(...) method after arView.scene.anchors.append(scene), not before. When you run the following code you'll see that model moves along the X axis during 2 sec, as expected.

let scene = try! Experience.loadBox()
guard let model = scene.steelBox?.children[0] as? ModelEntity
else { return }

var transform = Transform()
transform.translation.x = 0.5
arView.scene.anchors.append(scene)

DispatchQueue.main.async {
    model.move(to: transform, relativeTo: nil, duration: 2.0)
}

However, if you do not use the duration parameter (i.e. duration = 0.0 sec), the model will instantly move 0.5 m along the X axis.

DispatchQueue.main.async {
    model.move(to: transform, relativeTo: nil)
}

CodePudding user response:

I've just realised that marking the init method with @MainActor did not cause move(to:relativeTo:duration:) to be called on the main thread, executing it within DispatchQueue.main.async{} closure did the trick.

  • Related