https://developer.apple.com/videos/play/wwdc2021/10074/ with example that apple provide, there's dive app. in app sampel code, there's animation sequence which is done by octopus entity.
I think this is the only way that I can do multiple animation with swift / USDZ form.
the problem is that I can't reproduce animation sequence, so, I tweaked a little bit of sample code.
firstly, when scene is up, on UnderwaterView.swift, load models function, uncomment if state so that setupOctopus is triggered.
so, when ar app is on, octopus is appear on scene.
I changed a little bit of hidingLocation so that after 3seconds of appear octopus, it move with animation.
but the problem is octopus is moving but it shows no animation... what may be the problem?
struct OctopusSystem: RealityKit.System {
init(scene: RealityKit.Scene) {}
func update(context: SceneUpdateContext) {
let scene = context.scene
for octopus in scene.performQuery(OctopusComponent.query) {
print("oc 1111")
guard octopus.isEnabled else { continue }
print("oc 2222")
guard var component = octopus.components[OctopusComponent.self] as? OctopusComponent else { continue }
// guard component.settings?.octopus.fearsCamera ?? false else { return }
print("oc 3333\(component.state)")
switch component.state {
case .hiding:
print("oc 44444")
guard let camera = scene.performQuery(CameraComponent.query).first(where: { _ in true }) else { continue }
print("oc 555")
let fearDistance: Float = 1.0
let distanceToCamera = octopus.distance(from: camera)
print("oc 5555")
guard distanceToCamera < fearDistance else { continue }
print("oc 6666")
var hidingLocation = octopus
hidingLocation.transform.translation.x=hidingLocation.transform.translation.x-0.01
print("oc 7777")
// let distance = octopus.distance(from: hidingLocation)
// guard distance > fearDistance else { continue }
// print("oc 88")
var duration = 30.5
print("oc du...")
// Animations
DispatchQueue.main.asyncAfter(deadline: .now() + 3){
do {
let fixedTime
= component.animations.crawl2Swim.definition.duration
+ component.animations.swim2crawl.definition.duration
duration = max(fixedTime, duration)
let swimDuration = duration - fixedTime
let swimCycleDuration = component.animations.swim.definition.duration
let swimCycles = Int(swimDuration / swimCycleDuration)
duration = fixedTime + Double(swimCycles) * swimCycleDuration
if let animation = try? AnimationResource.sequence(with: [
component.animations.crawl2Swim,
component.animations.swim.repeat(count: swimCycles),
component.animations.swim2crawl
]) {
print("oc playanimation")
octopus.playAnimation(animation)
}
}
octopus.move(to: hidingLocation.transform, relativeTo: nil, duration: duration)
}
// component.state = .swimming(duration)
case .swimming(var time):
time -= context.deltaTime
component.state = time < 0 ? .hiding : .swimming(time)
}
octopus.components.set(component)
}
}
}