Dvole
Dvole

Reputation: 5795

How to prevent physics body from changing x position in Sprite Kit

I have an endless runner game, my main character has a physics body, I use it to handle jumping.
Everything is great before he collides with obstacles. He starts shifting back, and I want him to stay on his x position at all times.

I tried setting the body dynamic to NO, but this stops jumping.

How do I go about this? I don't want character to move on the x axis.

Upvotes: 1

Views: 474

Answers (2)

JohnKasich_2016
JohnKasich_2016

Reputation: 183

you can also use constraints

let range = SKRange(lowerLimit: 300, upperLimit: 340)

let lockToCenter = SKConstraint.positionX(range, y: range)

node.constraints = [ lockToCenter ]

and set the lower and upper limits to the same value for it to be locked in position.

Upvotes: 0

Duck
Duck

Reputation: 36013

do this on the update method

- (void)didSimulatePhysics {

    CGPoint fixedXPos = myCharacter.position;
    fixedXPos.x = fixedX;

    [myCharacter setPosition:fixedXPos];
}

Upvotes: 2

Related Questions