Search code examples
iosswiftsprite-kitskspritenodeskphysicsbody

Disable rotation of SKNode in SpriteKit


I have a player so with a physics body as a circle. Think of it as having a flappy bird as SKNode with a circle as physics body. Now every time I apply an impulse I want the physics body to rotate but I want do not want the bird to rotate


I am using physicsbody.allowrotation = true but this rotates the node (bird) as well. I want the bird to not rotate but the physics body should rotate.


Solution

  • You can probably add SKConstraint to your SKSpriteNode limiting rotation.

    let birdSprite = SKSpriteNode(imageNamed: "bird.png")
    birdSprite.physicsBody = SKPhysicsBody(circleOfRadius: max(birdSprite.size.width / 2, birdSprite.size.height / 2))
    birdSprite.physicsBody.allowsRotation =  true
    birdSprite.constraints = [SKConstraint.zRotation(SKRange(lowerLimit:0 upperLimit:0))]