如何使用 zRotation 约束阻止我的角色过度旋转? [英] How to stop my character from over rotating with zRotation constraints?
问题描述
我在我正在开发的平台游戏中有一辆车,我希望能够允许旋转,以便它可以翻山越岭之类的.我想限制我的车辆在正负方向旋转 30 度.
I have a vehicle in a platformer game I'm working on that I want to be able to allow rotation so it can roll up hills and such. I want to limit my vehicle to 30 degrees rotation in the negative and positive direction.
问题是当我启用旋转并从悬崖或直边下降处滚下时,我的车辆从侧面翻倒并着地.我一直在寻找一种方法来将 zRotation 限制在每个方向上的设定度数.
The issue is when i have rotation enabled and roll off of a cliff or straight edge drop off, my vehicle flips over off the side and lands on its head. I've been searching for a way to limit the zRotation to a set number of degrees in each direction.
我尝试使用本教程中的 IK 约束部分:https://www.raywenderlich.com/129895/sprite-kit-inverse-kinematics-swift-2 在我的更新功能中,但没有效果.
I tried using the IK constraints part from this tutorial: https://www.raywenderlich.com/129895/sprite-kit-inverse-kinematics-swift-2 in my update function, but it had no effect.
然后我发现了这个:https://developer.apple.com/documentation/spritekit/skconstraint/1519706-zrotation#declarations
这似乎正是我所需要的,但我无法弄清楚如何实现它.任何建议将不胜感激!
that seems to be exactly what I need, but I can't quite figure out how to implement it. any advice would be appreciated!
推荐答案
这应该可以解决问题:
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]
这篇关于如何使用 zRotation 约束阻止我的角色过度旋转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!