Jakub Gawecki
Jakub Gawecki

Reputation: 1239

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

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.

Upvotes: 1

Views: 873

Answers (2)

Andy Jazz
Andy Jazz

Reputation: 58103

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)
}

Upvotes: 1

Jakub Gawecki
Jakub Gawecki

Reputation: 1239

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.

Upvotes: 0

Related Questions