0

I currently have an application that flies a ship node in Scenekit through space at a constant velocity. I implemented a d-pad system that rotates the ship based on touch movement, the end goal being that the user can rotate the ship as it flies through space and it will move in the direction in which you direct it to.

The problem I'm currently having is that the ship node's position is reset at the start of each new touch and only travels forward after the completion of the SCNAction.

The below code is the implementation of my d-pad to control the ship's direction:

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)
            }
        }
    }
}

Is anyone able to advise as to how I can get the ship to rotate while still flying through space?

1 Answers1

0

Ok - looks like you have answered it yourself, so we should close it out.

@LittleRocketMan -> Added a second SCNAction to my extension and it worked.

Voltan
  • 1,170
  • 1
  • 7
  • 16