我一直试图在用户长按屏幕时将多维数据集向前移动。我无法使其平稳移动。有什么方法可以仅将其向前移动(向负方向移动)并更新场景中每一帧的位置?

这是我到目前为止的内容:

var carLocation = SCNVector3(x:0, y: 0, z:-0.01)

func setupScene() {
        sceneView = self.view as? SCNView
        scene = SCNScene(named: "art.scnassets/Map.scn")
        sceneView.scene = scene
        let holdRecognizer = UILongPressGestureRecognizer()
        holdRecognizer.addTarget(self, action: #selector(GameViewController.hold(recognizer:)))
        sceneView.addGestureRecognizer(holdRecognizer)
    }

 @objc func hold(recognizer: UILongPressGestureRecognizer) {
        print("Held down!")
        let position = recognizer.location(in: sceneView)
        carLocation = SCNVector3(x:0, y: 0, z:-0.01)
        carNode.physicsBody?.velocity += carLocation
    }

最佳答案

我对您的印刷品进行了计数,以便您可以看到LongPress会重复自身...“很多”,这可能不是您长期想要的。因此,假设您创建了以下内容:

let p = SCNPhysicsBody(type: .dynamic, shape: nil)
p.isAffectedByGravity = false
shipNode.physicsBody = p


然后,这将使您的对象平滑地前后移动。

@objc func hold(recognizer: UILongPressGestureRecognizer) {
    print("Held down! \(count)")
    count += 1
    shipNode.physicsBody?.velocity = SCNVector3Make(0.0, 0, 0.51)
}
@objc func handleTap(_ gestureRecognize: UIGestureRecognizer) {
    shipNode.physicsBody?.velocity = SCNVector3Make(0.0, 0, -0.51)
}


除非那里有大量的对象,否则我看不出它变慢的任何原因。设置scnView.showsStatistics = true可以查看您的FPS。希望您不尝试在模拟器中运行Scenekit?

关于swift - 长按可向前 move SCNNode,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55014287/

10-13 01:25