How to stop my character from over rotating with z

2019-07-08 05:15发布

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条回答
▲ chillily
2楼-- · 2019-07-08 06:05

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]
查看更多
登录 后发表回答