嘿,我已经在xcode中导入了我的角色动画,我想在scenekit中用触摸移动它,让我们假设从A点到另一个点B.但它没有移动我有以下代码。不知道为什么它不起作用请帮助....谢谢
//character imported from blender
var scene = SCNScene(named: "art.scnassets/hero.dae")!
// touches for movement
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
var taps = event!.allTouches()
touchCount = taps?.count
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
touchCount = 0
}
func positionCameraWithHero() {
let hero = heroNode.presentationNode
var heroPosition = hero.position
let cameraDamping:Float = 0.3
var targetPosition = SCNVector3Make(heroPosition.x, 30.0, heroPosition.z + 20.0)
var cameraPosition = camera.position
var cameraXPos = cameraPosition.x * (1.0 - cameraDamping) + targetPosition.x *
cameraDamping
var cameraYPos = cameraPosition.y * (1.0 - cameraDamping) + targetPosition.y *
cameraDamping
var cameraZPos = cameraPosition.z * (1.0 - cameraDamping) + targetPosition.z *
cameraDamping
cameraPosition = SCNVector3(x: cameraXPos, y: cameraYPos, z: cameraZPos)
camera.position = cameraPosition
light.position = SCNVector3(x: heroPosition.x, y: 90, z: heroPosition.z +
40.0)
light.rotation = SCNVector4(x: 1, y: 0, z: 0, w: Float(-M_PI/2.8))
}
func renderer(aRenderer: SCNSceneRenderer, didSimulatePhysicsAtTime time: NSTimeInterval) {
let moveDistance = Float(10.0)
let moveSpeed = NSTimeInterval(1.0)
let currentX = heroNode.position.x
let currentY = heroNode.position.y
let currentZ = heroNode.position.z
if touchCount == 1{
let action = SCNAction.moveTo(SCNVector3Make(currentX, currentY, currentZ -
moveDistance), duration: moveSpeed);
heroNode.runAction(action)
}
else if touchCount == 2 {
let action = SCNAction.moveTo(SCNVector3Make(currentX, currentY, currentZ +
moveDistance), duration: moveSpeed)
heroNode.runAction(action)
}
答案 0 :(得分:2)
每次渲染帧时,您的代码都会尝试创建并运行新的SCNAction。这可能不是你想要的。
相反,在接收到触摸时创建一个SCNAction,持续时间为10秒。跑吧。该代码属于touchesBegan()
(或者touchesEnded()
)。
您可以使用SCNAction.moveBy(_:duration:)
或SCNAction.moveByX(_:y:z:duration:)
代替moveTo()
来缩短代码。你所谓的moveSpeed
实际上是一个时间间隔,而不是速度,这个术语可能会让未来的维护者感到困惑。
我没有看到positionCameraWithHero()
被调用过。它应该从renderer(didSimulatePhysicsAtTime:)
。
注意:在您当前的实现中,永远不会调用renderer(didSimulatePhysicsAtTime{)
。那是因为你没有动画运行(见SKScene becomes unresponsive while being idle)。还要注意OS X上忽略playing
属性中的明显错误(请参阅SceneKit SCNSceneRendererDelegate - renderer function not called)。但是,一旦您更正了SCNAction的创建,您将不会遇到此问题。