我目前有一个应用程序可以让 Scenekit 中的飞船节点以恒定的速度在太空中飞行。我实现了一个 d-pad 系统,它可以根据触摸移动来旋转飞船,最终目标是用户可以在飞船在太空中飞行时旋转它,并且它会朝着你指向的方向移动。
我目前遇到的问题是船节点的位置在每次新触摸开始时都会重置,并且仅在 SCNAction 完成后才会向前移动。
下面的代码是我的 d-pad 的实现来控制船的方向:
extension GameViewController {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
touch = touches.first!
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touch {
let touchLocation = touch.location(in: self.view)
if gameView.virtualDPad().contains(touchLocation) {
let middleOfCircleX = gameView.virtualDPad().origin.x + 75
let middleOfCircleY = gameView.virtualDPad().origin.y + 75
let lengthOfX = Float(touchLocation.x - middleOfCircleX)
let lengthOfY = Float(((touchLocation.y - middleOfCircleY) * (-1)))
let turnY = lengthOfX/47.746371
let turnX = lengthOfY/47.746371
let action = SCNAction.rotateTo(x: CGFloat(turnX), y: CGFloat(turnY), z: 0.0, duration: 2.0, usesShortestUnitArc: true)
shipMesh.runAction(action)
}
}
}
}
有没有人能告诉我如何让船在飞行的同时在太空中旋转?