How to stop my character from over rotating with zRotation constraints?

邮差的信 提交于 2019-12-10 23:31:04

问题


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.

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.

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.

Then I found this: 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!


回答1:


This should do the trick:

  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]


来源:https://stackoverflow.com/questions/44853745/how-to-stop-my-character-from-over-rotating-with-zrotation-constraints

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!