Skip to content

Instantly share code, notes, and snippets.

@drewolbrich
Created January 16, 2024 01:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drewolbrich/bb1cf4e7e75d61c8bee5d2ad37b9c8c4 to your computer and use it in GitHub Desktop.
Save drewolbrich/bb1cf4e7e75d61c8bee5d2ad37b9c8c4 to your computer and use it in GitHub Desktop.
Failed attempt #2 at Entity/move with a callback
import Foundation
import RealityKit
import Combine
private var playbackCompletedSubscriptions: Set<AnyCancellable> = .init()
extension Entity {
/// Animated `Entity/move` with a completion handler.
///
/// This implementation works but for some reason `timingFunction` appears to be
/// ignored and the timing function of the resulting animation is always linear.
func move1(to target: Transform, relativeTo referenceEntity: Entity?, duration: TimeInterval, delay: TimeInterval = 0, timingFunction: AnimationTimingFunction = .default, completion: @escaping () -> Void) {
guard scene != nil else {
completion()
return
}
let animation = FromToByAnimation(name: "Entity/move", to: target, duration: duration, timing: timingFunction, isAdditive: false, bindTarget: .transform, delay: delay)
do {
let animationResource: AnimationResource = try .generate(with: animation)
let animationPlaybackController = playAnimation(animationResource)
scene?.publisher(for: AnimationEvents.PlaybackCompleted.self)
.filter { $0.playbackController == animationPlaybackController }
.sink(receiveValue: { event in
completion()
}).store(in: &playbackCompletedSubscriptions)
} catch {
assertionFailure("Could not generate animation: \(error.localizedDescription)")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment