Hi go easy on me as I'm still pretty new to swift. I'm creating a game in which your character (on the left of the screen) must swipe up or down to change lanes (4 lanes) or jump by tapping the right side of the screen, in order to dodge obstacles coming from the right side of the screen. This has presented the problem that when the character sprite is in one lane, some of it's pixels are in the lane above, so being in a different lane doesn't shield the character from collision. (This problem also exists for when the character jumps) Is there a way to program it so the character and obstacles only collide if they are in the same lane?
I've tried to make it so the .collisionBitMask and .categoryBitMask change depending on the position of either the obstacle or character but I can't figure out how to code it correctly. I'm pretty lost to be honest. Here is some code:
runningDoggo.physicsBody = SKPhysicsBody(texture: doggoTexture, size: runningDoggo.size)
runningDoggo.physicsBody?.affectedByGravity = false
runningDoggo.physicsBody?.isDynamic = true
runningDoggo.physicsBody?.collisionBitMask = 0x1 << 2
bush.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "8-bit bush"), size: bush.size)
bush.physicsBody?.isDynamic = true
bush.physicsBody?.affectedByGravity = false
bush.physicsBody?.categoryBitMask = 0x1 << 1
And then I have a set of switch cases in a function for randomly selecting which lane the bush comes in
switch randomLane{
case 1:
bush.position = CGPoint(x: 0.4*UIScreen.main.bounds.width, y:-0.07*UIScreen.main.bounds.height)
if(bush.position.y < runningDoggo.position.y) {
bush.zPosition = 6
}
self.addChild(bush)
case 2:
bush.position = CGPoint(x: 0.4*UIScreen.main.bounds.width, y:-0.18*UIScreen.main.bounds.height)
if(bush.position.y < runningDoggo.position.y) {
bush.zPosition = 6
}
self.addChild(bush)
case 3:
bush.position = CGPoint(x: 0.4*UIScreen.main.bounds.width, y:-0.29*UIScreen.main.bounds.height)
if(bush.position.y < runningDoggo.position.y) {
bush.zPosition = 6
}
self.addChild(bush)
case 4:
bush.position = CGPoint(x: 0.4*UIScreen.main.bounds.width, y:-0.4*UIScreen.main.bounds.height)
if(bush.position.y < runningDoggo.position.y) {
bush.zPosition = 6
}
self.addChild(bush)
default:
print("How is this not 1,2,3, or 4?")
}
bush.run(moveAndRemoveBush)
I want it to only collide (EDIT: Not to collide but to contact) the runningDoggo sprite with the bush sprite if they were in the same lane and pass by without colliding if the character isn't in the obstacle's lane.