SpriteKit SKPhysicsBody script in one direction, like a door, you can go through but not come back

In SpriteKit SKPhysicsBody, you can get an object through which you can pass, but not return.

The idea is that they donโ€™t collide in one direction, so you go through and donโ€™t go back like a hatch.

+5
source share
4 answers

Iโ€™m not entirely sure that physics is possible, but you should be able to interact with the bit mask to collide with the physical body during the game in order to achieve a similar effect.

So, you have the door open, and when it detects that the player is touching it *, it changes the bit mask so that the player collides with it. This should allow the player to go one way, but not return.

* In fact, find the door when the player no longer touches the door through the playerโ€™s physical body, and check the location of x or y depending on whether it is a hatch door or an ordinary door. If the location is far enough from the door, change the bit matrix of the door collision so that the player cannot pass.

+1
source

In your door-to-object contact test, you need to check the direction of movement of the object (you can use speed to get this). If the object moves in the direction in which the door locks them, then you turn on the collisionBitMask door on the node, otherwise you need to remove it from the collisionBitMask node

0
source

The easiest way to do this is to add two child sprites to the node, representing the top and bottom of the trap door. Thus, you can check in which direction the oncoming sprite arises, and enable / disable the dynamics from the other, if necessary.

0
source

The solution is only to change collisionBitMask

 func platformSolid() { self.physicsBody?.collisionBitMask = kBIT_MASK_PLAYER | kBIT_MASK_WALL | kBIT_MASK_PLATFORM } func platformThrough() { self.physicsBody?.collisionBitMask = kBIT_MASK_PLAYER | kBIT_MASK_WALL } 

self in this example is player bit-bit-bit

In the delegate:

 func didEndContact(contact: SKPhysicsContact) { if contact.bodyB.categoryBitMask == kBIT_MASK_PLATFORM { player.platformSolid() } } 

So, as soon as the player has passed (didEndContact), you will make the platform (door) solid.

0
source

Source: https://habr.com/ru/post/1242869/


All Articles