I try to make transitions between the animations of a character but I can't quite understand...
To start any of my character without animation in position T
Then I import a walking animation and add it to my character
To begin with, my character starts walking but his arms remain in T... why?
Here is my code
import PlaygroundSupport
import SwiftUI
import SceneKit
let scene = SCNScene(named: "Agnes.scn")!
let model = scene.rootNode
let speed:TimeInterval = 5
var run:SCNAnimationPlayer?
var dance:SCNAnimationPlayer?
var idle:SCNAnimationPlayer?
var anim:SCNAnimationPlayer?
struct CharacterView: UIViewRepresentable {
func makeUIView(context: Context) -> SCNView {
let scnView = SCNView(frame: .zero)
scnView.scene = scene
scnView.autoenablesDefaultLighting = true
scnView.allowsCameraControl = true
return scnView
}
func updateUIView(_ view: SCNView, context: Context) {
}
}
struct ContentView: View {
@State private var isRunning = false
@State private var isSalsa = false
@State private var isIdle = false
var body: some View {
VStack {
CharacterView()
.frame(height: 300)
HStack {
Button(action: {
anim?.stop(withBlendOutDuration: speed)
run?.play()
anim = run
}) {
Text("Running")
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(10)
}
Button(action: {
anim?.stop(withBlendOutDuration: speed)
dance?.play()
anim = dance
}) {
Text("Salsa")
.padding()
.background(Color.green)
.foregroundColor(.white)
.cornerRadius(10)
}
Button(action: {
anim?.stop(withBlendOutDuration: speed)
idle?.play()
anim = idle
}) {
Text("Idle")
.padding()
.background(Color.orange)
.foregroundColor(.white)
.cornerRadius(10)
}
}
.padding(.top, 20)
}
.onAppear {
// Mettez ici le code pour charger les animations du personnage
run = Page_Contents.animation(name: "Running.usdz")
dance = Page_Contents.animation(name: "Dancing.usdz")
idle = Page_Contents.animation(name: "Idle.usdz")
}
}
}
func animation(name:String) -> SCNAnimationPlayer
{
var animation:SCNAnimationPlayer? = nil
let fichier = SCNScene( named: name )!
fichier.rootNode.enumerateChildNodes { (child, stop) in
if !child.animationKeys.isEmpty {
animation = child.animationPlayer(forKey: child.animationKeys[0])
}
}
let cle:String = name
model.addAnimationPlayer(animation!, forKey:cle)
animation?.animation.isRemovedOnCompletion = false
animation?.animation.blendInDuration = speed
animation?.animation.blendOutDuration = speed
animation?.animation.isRemovedOnCompletion = false
animation?.stop()
return animation!
}
PlaygroundPage.current.setLiveView(ContentView())
And my Playground
Thanks !