Search code examples
iosswiftsprite-kitskphysicsbodyskscene

How to remove physicsBody/ stop responding to gravity after sprite reaches a specific position?


So I want my sprite to move with gravity until it reaches a specific location and then it should stop responding to gravity and stay there. How can I do that? Thanks in advance

I want it so that it stops once it reaches target position

sprite.physicsBody = SKPhysicsBody(circleOfRadius: sprite.size.width/2)
if sprite.position == newPosition {
sprite.physicsBody?.affectedByGravity = false}

doesn't work


Solution

  • Try doing something like this

     override func update(_ currentTime: TimeInterval) {
        //In this case, you can just change self.size.height/2 with whatever position you want.
        if sprite.position.y <= self.size.height/2{
    
            sprite.physicsBody?.affectedByGravity = false
            sprite.physicsBody?.isDynamic = false
    
    
        }
    
    
    }
    

    In this case, I created a sprite that is positioned way up top with gravity set to true. In override func update, I am stating that once my sprite has reached this certain location, I want it to be static.