【问题标题】:How to stop my character from over rotating with zRotation constraints?如何阻止我的角色在 zRotation 约束下过度旋转?
【发布时间】:2017-06-30 19:36:39
【问题描述】:

我正在开发一款平台游戏中的载具,我希望它能够允许旋转,以便它可以滚上山坡等。我想将我的车辆限制在正负方向旋转 30 度。

问题是当我启用旋转并从悬崖上滚下或从直边下落时,我的车辆从侧面翻转并落在其头上。我一直在寻找一种方法来将 zRotation 限制在每个方向上设定的度数。

我尝试在我的更新函数中使用本教程中的 IK 约束部分:https://www.raywenderlich.com/129895/sprite-kit-inverse-kinematics-swift-2,但没有效果。

然后我发现了这个:https://developer.apple.com/documentation/spritekit/skconstraint/1519706-zrotation#declarations

这似乎正是我所需要的,但我无法完全弄清楚如何实现它。任何建议将不胜感激!

【问题讨论】:

  • 使用zRotation 属性时,请确保为它分配一个弧度值...如果这不是问题,请发布相关代码..
  • 我不知道我需要什么代码。我知道 zRotation 以弧度为单位,但 IK 约束以度为单位。我更感兴趣的是了解如何使用限制旋转的苹果 zRotation 函数。

标签: ios swift swift3 sprite-kit


【解决方案1】:

这应该可以解决问题:

  let thirtyDegrees = CGFloat(0.523599) // Convert degrees to rads.

  let rotationRange = SKRange(lowerLimit: -thirtyDegrees, upperLimit: thirtyDegrees)

  let rotationConstraint = SKConstraint.zRotation(rotationRange)

  let vehicle = SKSpriteNode()

  vehicle.constraints = [rotationConstraint]

【讨论】:

  • 我把它放在我的播放器类中,效果很好。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-27
相关资源
最近更新 更多