Hello Aditya7,
If you use Entity.loadModel, this will return a ModelEntity, it will not load the animations that may be in the usdz file. Instead you should use Entity.load, which will return an Entity, and will load the animations that may be in the usdz file.
Additionally, you should note that calling playAnimation before the Entity isActive in the scene will result in the resulting AnimationPlaybackController immediately setting to an isComplete status, so the animation won't play when the Entity is added to the scene.
The following snippet demonstrates how you can handle this situation:
Code Block // Load the usdz as an Entity |
entity = try! Entity.load(contentsOf: url) |
|
// Add the entity to an anchor, add the anchor to the scene. The anchor might not be anchored in the scene at this point. |
anchor.addChild(entity) |
arView.scene.anchors.append(anchor) |
|
// Subscribe to AnchoredStateChanged events, play the animations of the anchor's children once it is active in the scene. |
arView.scene.subscribe(to: SceneEvents.AnchoredStateChanged.self) { [self] (event) in |
if anchor.isActive { |
for entity in anchor.children { |
for animation in entity.availableAnimations { |
entity.playAnimation(animation.repeat()) |
} |
} |
} |
}.store(in: &cancellables) // Remember to store the cancellable! |