aignetti
aignetti

Reputation: 491

Swift SpriteKit - zRotation in SKAction follow path

im working on a game and i have now a question to the zRotation of a SpriteNode while following a path.

So i have a ShapeNode which looks like this: enter image description here

Im using a UIBezierPath for this Shape.

Now i have a SpriteNode which starts in the middle of this path and moves randomly from the left to the right and otherwise.

For this i have 2 functions:

func FischRechtsBewegen(){ //for moving right
    FischNode1.removeAllActions()
    let BewegeRechts = SKAction.follow(FischKurveRechts.cgPath, asOffset: false, orientToPath: true, duration: 6)
    FischNode1.run(BewegeRechts)
}

func FischLinksBewegen(){ //for moving left
    FischNode1.removeAllActions()
    let BewegeLinks = SKAction.follow(FischKurveLinks.cgPath, asOffset: false, orientToPath: true, duration: 6)
    FischNode1.run(BewegeLinks)
}

Also in this functions im calculating for each change of the direction a new path for the SpriteNode. (but as this code is not important for my question i removed it in my example code here)

This new path (for the SKAction) begins at the actual position of the SpriteNode and then goes to the right (if the new movement is right) or to the left (if the new movement is left) with an curve to the old start/endpoint.

My problem is now: When moving to the left the SpritesNodes zRotation is exactly how i want it. The node "looks" to the top of the screen. When moving to the right the SpriteNodes zRotation is not good - now the node points to the bottom of the screen which is very ugly.

Ive tried different things - i have changed the image i use for the SpriteNode (the rotation there). I have tried to change the zRotation of the SpriteNode with an action. Nothing helps. Of course because in my SKAction i am saying "orientToPath = true". Im asking now .. is there an easy way to get the SpriteNode always "looking" to the top of the screen or do i have to change the zRotation manually in the update() function?

Im asking because there is this property in the SKAction.follow (orientToPath) and im asking myself if i could use it in this case but if there is a way to change it just a little bit for my need. Or if this is not possible - if the path is going to the left why is the orientToPath different to the orientToPath in the path which is going to the right?

Here is an video of my problem, maybe its easier to see then what i mean. https://youtu.be/vLH5nUlE91U

Upvotes: 1

Views: 694

Answers (1)

Alessandro Ornano
Alessandro Ornano

Reputation: 35382

You can try to reverse your UIBezierPath with the method:

public func bezierPathByReversingPath() -> UIBezierPath

instead of reverse followPath action, and monitoring this action with a boolean that know when you are to the clockwise sense or anticlockwise sense.

Upvotes: 1

Related Questions