好的,所以我使用操纵杆直接关闭Apple的教程,为SCNNode
here.移动SceneKit
我复制了代码并获得了操纵杆以移动和旋转角色 - 但不是同时进行,而不是相对于节点的正确方向。
所有正确的代码都在下载中,但我所做的就是从操纵杆UI获取操纵杆手柄和float2的角度偏移 -
characterDirection = float2(Float(padNode.stickPosition.x), -Float(padNode.stickPosition.y))
let direction = theDude.characterDirection(withPointOfView: renderer.pointOfView)
directionAngle = CGFloat(atan2f(direction.x, direction.z))
public func characterDirection(withPointOfView pointOfView: SCNNode?) -> float3 {
let controllerDir = theDude.direction //THIS ISNT BEING UPDATED
if controllerDir.allZero() {
return float3.zero
}
var directionWorld = float3.zero
if let pov = pointOfView {
let p1 = pov.presentation.simdConvertPosition(float3(controllerDir.x, 0.0, controllerDir.y), to: nil)
let p0 = pov.presentation.simdConvertPosition(float3.zero, to: nil)
directionWorld = p1 - p0
directionWorld.y = 0
if simd_any(directionWorld != float3.zero) {
let minControllerSpeedFactor = Float(0.2)
let maxControllerSpeedFactor = Float(1.0)
let speed = simd_length(controllerDir) * (maxControllerSpeedFactor - minControllerSpeedFactor) + minControllerSpeedFactor
directionWorld = speed * simd_normalize(directionWorld)
}
}
return directionWorld
}
我没有写下最后一部分,仍然试图理解它。但是相关的是我有一个float3和一个角度,当我尝试在我的渲染器更新SCNActions
中以func
运行它们时它们是冲突的:
这是Apple在更新中的基本内容:
// move
if !direction.allZero() {
theDude.characterVelocity = direction * Float(characterSpeed)
var runModifier = Float(1.0)
theDude.walkSpeed = CGFloat(runModifier * simd_length(direction))
// move character - IMPORTANT
theDude.directionAngle = CGFloat(atan2f(direction.x, direction.z))
theDude.node.runAction(SCNAction.move(by: SCNVector3(theDude.characterDirection(withPointOfView: theDude.node)), duration: TimeInterval(40))) //HERE - random time
theDude.isWalking = true
} else {
theDude.isWalking = false
theDude.node.removeAllActions()
}
}
在注释行的哪个地方,我应用了移动,这里Apple应用了旋转:
var directionAngle: CGFloat = 0.0 {
didSet {
theDude.node.runAction(
SCNAction.rotateTo(x: 0.0, y: directionAngle, z: 0.0, duration: 0.1, usesShortestUnitArc:true))
}
}
它们都在发生,问题是我不知道该放什么作为我的时间和我的节点移动说,当我有操纵杆指向右边时,等等因为我没有正确地进行移动。
我试图复制演示,但他们有一个移动的地板,所以它是不同的。我在这里做错了什么?