嘿,我已经在xcode中导入了我的角色动画,我想在scenekit中用touch-in-scenekit-let将其从点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)
    }

最佳答案

您的代码试图在每次呈现帧时创建并运行一个新的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的创建,就不会有这个问题。

关于ios - 角色不在场景工具箱中移动,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35555198/

10-13 09:27