Swift场景套件:将SCNNode移动到分接位置?

时间:2018-02-10 02:21:53

标签: ios swift scenekit arkit scnvector3

好的,在这里使用Swift中的ARKit并尝试抓住它 -

对于我的游戏控件我希望能够控制当用户手指在屏幕上向下时SCNNode正朝着的点(在3d空间中的位置),这意味着由touchesBegan func启动。

我想让它像苹果公司的狐狸游戏一样用她的操纵杆,但在AR:https://developer.apple.com/library/content/samplecode/Fox/Introduction/Intro.html

我的主要问题是我的SCNAction的移动位置似乎没有在touchesMoved上正确更新,而且我需要SCNNode以相同的速度移动到该位置而不管它有多远离开了。

这是我拥有的,它有效但不正确:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        guard let touch = touches.first else { return }

        let results = sceneView.hitTest(touch.location(in: sceneView), types: [ARHitTestResult.ResultType.featurePoint])
        guard let hitFeature = results.last else { return }


        checkIfNodeTapped(touches: touches, node: theDude.node)
        theDude.moveToPos(pos: getARPos(hitFeature: hitFeature))
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

        guard let touch = touches.first else { return }

        let results = sceneView.hitTest(touch.location(in: sceneView), types: [ARHitTestResult.ResultType.featurePoint])
        guard let hitFeature = results.last else { return }

        theDude.updateWalkTo(pos: getARPos(hitFeature: hitFeature))

    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
//        if virtualObjectManager.virtualObjects.isEmpty {
//
//            return
//        }
//        virtualObjectManager.reactToTouchesEnded(touches, with: event)

        //Remove move actions
        theDude.stopMoving()

    }

func updateWalkTo(pos: SCNVector3)
    {
        walkAction = SCNAction.move(to: pos, duration: 1)
    }

    func moveToPos(pos: SCNVector3)
    {
        walkAction = SCNAction.move(to: pos, duration: 1)
        self.node.runAction(walkAction, forKey: "walk")

    }

    func stopMoving()
    {
        self.node.removeAction(forKey: "walk")
    }

其中walkAction只是一个已定义的SCNAction。如何解决此问题,以便节点向用户手指在屏幕上的任何位置(转换为AR点)运行?

0 个答案:

没有答案