我想让我的物体(SCNNode)面向它移动的点进行旋转。目前我只在x和y轴上移动,我尝试过以下方法:
// Action to move node
let action1 = SCNAction.moveTo(SCNVector3(x: 4.0, y: 0.0, z: 6.0), duration: 3)
// Get the tan angle:
let angle = CGFloat(atan2(z, x))
if angle - previousAngle > Pi {
playerAngle += 2 * Pi
} else if previousAngle - angle > Pi {
playerAngle -= 2 * Pi
}
previousAngle = angle
playerAngle = angle * RotationBlendFactor + playerAngle * (1 - RotationBlendFactor)
let rot = playerAngle - 90 * DegreesToRadians
flyNode.rotation = SCNVector4(x: 0, y: 1, z: 0, w: Float(rot))
它适用于某些目标点,但不适用于所有目标点。
我尝试将SCNLookAtConstraint添加到限制数组中,它可以完美地旋转我的节点,但它会停止我的移动动画:
let targerNode = SCNNode()
targerNode.position = SCNVector3(x: -4.0, y: 0.0, z: -2.0)
let con = SCNLookAtConstraint(target: targerNode)
flyNode.constraints = [con]
let action1 = SCNAction.moveTo(SCNVector3(x: 4.0, y: 0.0, z: -2.0), duration: 3)
你能帮我解决这个问题吗?