我一直在尝试当用户长按屏幕时向前 move 立方体。我无法让它顺利 move 。有什么方法可以将其仅向前 move (朝负方向)并更新场景中每一帧的位置?
这是我到目前为止所拥有的:
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
然后这将使您的物体顺利地向前和向后 move 。
@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/