Search code examples
swiftpathsprite-kitskaction

Restrict node movement along path in Swift


On SpriteKit, there is a SKAction follow that using for node to follow a path you create with CGMutablePath. Like this one :

path = CGMutablePath()
path?.move(to: point1)
path?.addLine(to: point2)

let follow = SKAction.follow(path!, asOffset: false, orientToPath: true, speed: speedPath!)

But I want to make player to move node free along the path, not some action that follow path. Like there is a path with straight line or ellipse using bezier path and player could move the node along the path. Here is the images :

enter image description here

Any idea about this?


Solution

  • After looking for a while, I finally got what I want. Basically, you need to get each point on the path, whether it is a line or bezier. Then you can move node along the closest points.

    Here is the source: Find Nearest point