当我点击屏幕或摇动时,我试图创造一种手工掷骰子的效果。在我的参考代码中有旋转效果,以便骰子面临随机变化。我试图在骰子旋转时制作这个代码,它必须是摔倒。我的意思是它会从预定义的坐标y下降到坐标y 0。任何想法都会受到影响。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first{
let touchLocation = touch.location(in: sceneView)
let results = sceneView.hitTest(touchLocation, types: .existingPlaneUsingExtent)
if let hitResult = results.first{
let diceScene = SCNScene(named: "art.scnassets/diceCollada.scn")!
if let diceNode = diceScene.rootNode.childNode(withName: "Dice", recursively: false){
diceNode.position = SCNVector3(
hitResult.worldTransform.columns.3.x,
hitResult.worldTransform.columns.3.y + diceNode.boundingSphere.radius,
hitResult.worldTransform.columns.3.z)
let randomX = Float(arc4random_uniform(4) + 1) * (Float.pi/2)
let randomZ = Float(arc4random_uniform(4) + 1) * (Float.pi/2)
diceNode.runAction(
SCNAction.rotateBy(x: CGFloat(randomX * 3),
y: 0,
z: CGFloat(randomZ * 3),
duration: 0.5)
)
sceneView.scene.rootNode.addChildNode(diceNode)
}
print(hitResult)
}
}
}