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
Post
Replies
Boosts
Views
Activity
Im searching for a simple swiftUI code that can move a cube in immersive space when I click the X button on Sony Playstation Game Controller,
I did load a USD entity and connect the game controller but when it comes to get a simple click into the reality view to change position or rotate the entity it always not even accepted in codes. although I can control it with a finger or hand gestures, and yes I have closed the "Send Game Controller to device" in the simulator to make sure the controller would not effect the system but only effect in the game,
here is a sample
import SwiftUI
import RealityKit
import GameController
struct ImmersiveView: View {
@State var entity = ModelEntity()
@State var z: Float = -4.0
var body: some View {
RealityView { content in
if let cube = try? await ModelEntity(named: "am.usdz") {
entity = cube
entity.generateCollisionShapes(recursive: true)
entity.name = "Cube"
entity.position = [0, 0, -4]
entity.components.set(InputTargetComponent(allowedInputTypes: .indirect))
entity.components.set(HoverEffectComponent())
content.add(entity)
}
}
.gesture(
SpatialTapGesture()
.targetedToEntity(entity)
.onEnded { value in
print(value)
z -= 1.0
entity.position = [0, 0, z]
}
)
// What to do here to make game controller button X act as the gesture do
}
}
I Have a multiple if else statements under another if else statements and I want to be able to fold all child statements under certain parent without have to fold each one separately cause its like 100's of them example:
This Is Unfolded
func example(v1: String, v2: Int, v3: Int)
{
if v1 == "Food"
{
if v2 == 2 && v3 == 7
{
// Some Long Lines Codes
}
else if v2 == 2 && v3 == 33
{
// Some Long Lines Codes
}
else if v2 == 44 && v3 == 72
{
// Some Long Lines Codes
}
else if v2 == 12 && v3 == 7
{
// Some Long Lines Codes
}
else if v2 == 102 && v3 == 97
{
// Some Long Lines Codes
}
}
}
And I Wanted To Be Folded Like That With A Simple Click Not Fold It One By One
func example(v1: String, v2: Int, v3: Int)
{
if v1 == "Food"
{
if v2 == 2 && v3 == 7
{...}
else if v2 == 2 && v3 == 33
{...}
else if v2 == 44 && v3 == 72
{...}
else if v2 == 12 && v3 == 7
{...}
else if v2 == 102 && v3 == 97
{...}
}
}
Imaging like 100 lines of those else if conditions that i need to fold each one manually using CMD + Opt + <- or even using the arrow beside, I hope there is a fast and reliable shortcut or a click to help folding child codes or selected codes
btw I know there is a cmd + opt + shift + <- to fold all but that's not helping cause when I unfold the function I would see all remains as it was before.
I hope anyone could have a clue or if there is an ability to add a script function to do so.