Search code examples
sprite-kitswiftskphysicsbody

SpriteKit detect collision without setting dynamic to true?


I want my sprites collisions and contacts to be detected, but I don't want them to move dynamically (I just need to know that they've touched).

didBeginContact(contact: SKPhysicsContact!) is only called if I set my player's physicsBody.dynamic to true. How can I get these delegate method calls without effecting the position or movement of my player?


Solution

  • You can pin objects on the screen, so you don't need to set the gravity to 0 (If you want to keep the gravity for other objects). Set the object up like this:

        object.physicsBody.dynamic = true
        object.physicsBody.affectedByGravity = false
        object.physicsBody.pinned = true
    

    With this setup your object can collide with other objects without moving.