How can i play a USDZ entity animation in reverse? I have tried to put a negative value to the speed as I was doing in SceneKit to make the animation reverse play but it did not work. here is my code:
import SwiftUI
import RealityKit
struct ImmersiveView: View {
@State var entity = Entity()
@State var openDoor: Bool = true
var body: some View {
RealityView { content in
if let mainDoor = try? await Entity(named: "Door.usdz") {
if let frame = mainDoor.findEntity(named: "DoorFrame")
{
frame.position = [0, 0, -8]
frame.orientation = simd_quatf(angle: (270 * (.pi / 180)), axis: SIMD3(x: 1, y: 0, z: 0))
content.add(frame)
entity = frame.findEntity(named: "Door")!
entity.components.set(InputTargetComponent(allowedInputTypes: .indirect))
entity.components.set(HoverEffectComponent())
let entityModel = entity.children[0]
entityModel.generateCollisionShapes(recursive: true)
}
}
}
.gesture(
SpatialTapGesture()
.targetedToEntity(entity)
.onEnded { value in
print(value)
if openDoor == true
{
let animController = entity.playAnimation(entity.availableAnimations[0], transitionDuration: 0 , startsPaused: true)
animController.speed = 1.0
animController.resume()
openDoor = false
}
else
{
let animController = entity.playAnimation(entity.availableAnimations[0], transitionDuration: 0 , startsPaused: true)
animController.speed = -1.0 // it does not work to reverse
animController.resume()
openDoor = true
}
}
)
}
}
The Door should open with first click which is already happening and close with second click which is not happening as it does not reverse play the animation
Setting an AnimationDefinition's speed property to a negative value will cause its associated animation to play in reverse. For example:
var reversedDefinition = animationResource.definition
reversedDefinition.speed = -1
let reversedAnimation = try AnimationResource.generate(with: reversedDefinition)
entity.playAnimation(reversedAnimation)