Skip to content

Instantly share code, notes, and snippets.

@drewolbrich
Created January 16, 2024 01:54
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/f900bef53a3b3e9218d5bbfb967536b4 to your computer and use it in GitHub Desktop.
Save drewolbrich/f900bef53a3b3e9218d5bbfb967536b4 to your computer and use it in GitHub Desktop.
Failed attempt #1 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 doesn't work. The animation doesn't happen,
/// although `completion` is called after `duration` seconds.
func move2(to target: Transform, relativeTo referenceEntity: Entity?, duration: TimeInterval, delay: TimeInterval = 0, timingFunction: AnimationTimingFunction = .default, completion: @escaping () -> Void) {
guard scene != nil else {
completion()
return
}
let animationPlaybackController = move(to: transform, relativeTo: referenceEntity, duration: duration, timingFunction: timingFunction)
scene?.publisher(for: AnimationEvents.PlaybackCompleted.self)
.filter { $0.playbackController == animationPlaybackController }
.sink(receiveValue: { event in
completion()
}).store(in: &playbackCompletedSubscriptions)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment