2017-03-13 15 views
0

プレイヤーノードを各クリックに向けて回転させる方法を理解するのに問題があります。 Iveは既にx-z平面上のクリックの切片を得て、それに移動するノードを設定し、キャラクタが動くxとzの平面と比較してクリックのラジアンを見つけました。文字が0回転していないと、私の方法は動作しません。私は、これを行うより簡単な方法を推測しています。誰かが私にいくつかのドキュメントを教えてもらうか、プレーヤーの現在の方向とクリック角度との違いを把握して、動きに向かう方法を教えてください。Scenekitのノードローテーション

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

    if game.state == .tapToPlay { 
     startGame() 
    } 

    if game.state == .playing { 

     let touch = touches.first! 
     let location = touch.location(in: scnView) 
     let hitResults = scnView.hitTest(location, options: nil) 

     if hitResults.first?.node.name == "Grass" { 
      //pigNode.runAction(SCNAction.move(to: (hitResults.first?.localCoordinates)!, duration: 1.0)) 
      let moveAction = SCNAction.move(to: (hitResults.first?.localCoordinates)!, duration: 1.0) 
      let location = hitResults.first?.localCoordinates 

      /* 
      pigNode.runAction(SCNAction.rotateTo(x: 0.0, 
               y: CGFloat(offset.y), 
               z: 0.0, duration: 1.0, usesShortestUnitArc: true)) 

      let rotateAction = SCNAction.rotateTo(x: CGFloat((locationOnPlane?.x)!), 
                y: CGFloat((locationOnPlane?.y)!), 
                z: CGFloat((locationOnPlane?.z)!), 
                duration: 1.0, usesShortestUnitArc: true) 
      */ 
      let locationOnPlane = CGPoint(x: Double((location?.x)!), y: Double((location?.z)!)) 
      let offset = CGPoint(x: Double(locationOnPlane.x) - Double(pigNode.position.x), y: Double(locationOnPlane.y) - Double(pigNode.position.z)) 
      let length = sqrt(offset.x * offset.x + offset.y * offset.y) 
      let direction = CGPoint(x: offset.x/length, y: offset.y/length) 

      let rotationAngle = CGFloat(atan2(direction.y, direction.x)) 

      let rotateAction = SCNAction.rotate(by: rotationAngle, around: SCNVector3(x: 0.0, y: 1.0, z: 0.0), duration: 1.0) 
      //let rotateAction = SCNAction.rotateBy(x: 0.0, y: rotationAngle, z: 0.0, duration: 1.0) 
      //let rotateAction = SCNAction.rotateTo(x: 0.0, y: rotationAngle, z: 0.0, duration: 1.0, usesShortestUnitArc: true) 

      print(rotationAngle) 
      print(pigNode.eulerAngles.y) 



      let groupAction = SCNAction.group([moveAction, rotateAction]) 
      pigNode.runAction(groupAction) 
     } 
    } 
} 
+0

が、私はそれを考え出し考えるのに役立ちます願っています。私がXZ平面上の角度を計算する方法は、ノードがその軸に沿って向いているときに角度が0なのでXY平面であればZは実際にXです。ちょうどlocationOnPlane.xを場所z値に変更しましたそれに最も短い単位円弧が使用されているとコメントしたrotateAction thatsを使用しました。 – anthony

答えて