Sprite Kit camera smooth movement with delay

Viewed 658

So, I can make camera move with player, but it doesn't act the way I need it. I have set word boundaries:

func keepPlayerInBounds() {
    if player.position.x < frame.minX + player.size.width/2 {
        player.position.x = frame.minX + player.size.width/2
    }

    if player.position.x > frame.maxX + player.size.width/2 {
        player.position.x = frame.maxX + player.size.width/2
    }
}

So I need camera max and min X to be to as max and mix X player position in the worldNode. And move it smooth with delay as player moves right or left. I was trying to set:

override func didFinishUpdate()
    cam.position.x = player.position.x
}

Or:

override func didFinishUpdate() {
    let move = SKAction.moveTo(x: player.position.x, duration: 0.5)
    cam.run(move)
}

But it brings more headache than needed.

The question is: how to set left and right max position to the camera, and move it with delay without bugs. I spend almost three weeks to find the answer, but still got nothing. Thanks!

1 Answers
Related