swift sprite kit game when I shoot on a enemy sometimes the bullet goes trough the enemy, how can I fix this? -


i'm making game in sprite kit (2d).

i have code:

meteor.physicsbody = skphysicsbody(rectangleofsize: enemy.size) 

and have meteor image need destroy when shoot on device meteor bullet goes through meteor bug or did wrong? , how can fix issue?

thanks reading problem, hope can me! if don't understand question plz comment don't understand.

func firebullet() {      let bullet = skspritenode(imagenamed: "bullet")     bullet.name = "bullet"     bullet.setscale(2.9)     bullet.position = player.position     bullet.zposition = 1     bullet.physicsbody = skphysicsbody(rectangleofsize: bullet.size)     bullet.physicsbody!.affectedbygravity = false     bullet.physicsbody!.categorybitmask = physicscategories.bullet     bullet.physicsbody!.collisionbitmask = physicscategories.none     bullet.physicsbody!.contacttestbitmask = physicscategories.meteor     self.addchild(bullet)      let movebullet = skaction.movetoy(self.size.height + bullet.size.height, duration: 1)     let deletebullet = skaction.removefromparent()     let bulletsequence = skaction.sequence([bulletsound, movebullet, deletebullet])     bullet.runaction(bulletsequence)         }  func spawnmeteor(){      let randomxstart = random(min: cgrectgetminx(gamearea), max: cgrectgetmaxx(gamearea))     let randomxend = random(min: cgrectgetminx(gamearea), max: cgrectgetmaxx(gamearea))      let startpoint = cgpoint(x: randomxstart, y: self.size.height * 1.2)     let endpoint = cgpoint(x: randomxend, y: -self.size.height * 0.2)      let meteor = skspritenode(imagenamed: "meteor\(arc4random_uniform(2))")     meteor.name = "meteor"     meteor.setscale(0.2)     meteor.position = startpoint     meteor.zposition = 2     meteor.physicsbody = skphysicsbody(rectangleofsize: meteor.size)     meteor.physicsbody!.affectedbygravity = false     meteor.physicsbody!.categorybitmask = physicscategories.meteor     meteor.physicsbody!.collisionbitmask = physicscategories.none     meteor.physicsbody!.contacttestbitmask = physicscategories.player | physicscategories.bullet      self.addchild(meteor)      let movemeteor = skaction.moveto(endpoint, duration: 2)     let deletemeteor = skaction.removefromparent()     let losealifeaction = skaction.runblock(losealife)     let meteorsequence = skaction.sequence([movemeteor, deletemeteor, losealifeaction])        if currentgamestate == gamestate.ingame{     meteor.runaction(meteorsequence)     }      let dx = endpoint.x - startpoint.x     let dy = endpoint.y - startpoint.y     let amounttorotate = atan2(dy, dx)     enemy.zrotation = amounttorotate  }   func didbegincontact(contact: skphysicscontact) {      var body1 = skphysicsbody()     var body2 = skphysicsbody()       if contact.bodya.collisionbitmask < contact.bodyb.categorybitmask{         body1 = contact.bodya         body2 = contact.bodyb     }     else{         body1 = contact.bodyb         body2 = contact.bodya     }      if body1.categorybitmask == physicscategories.player && body2.categorybitmask == physicscategories.meteor{         //if player has hit meteor          if body1.node != nil {         spawnexplosion(body1.node!.position)         }            if body2.node != nil {         spawnexplosion(body2.node!.position)         }           body1.node?.removefromparent()         body2.node?.removefromparent()           rungameover()       }      if body1.categorybitmask == physicscategories.bullet && body2.categorybitmask == physicscategories.meteor && body2.node?.position.y < self.size.height {         //if bullet has hit meteor            addscore()           if body2.node != nil{         spawnexplosion(body2.node!.position)         spawnplusscore(body2.node!.position)         }           body1.node?.removefromparent()         body2.node?.removefromparent()       } 

spritekit doesn't perform precise collision detection because aims achieve faster performance. can make physicsbody's property usesprecisecollisiondetection true, this:

    bullet.physicsbody!.usesprecisecollisiondetection = true 

https://developer.apple.com/reference/spritekit/skphysicsbody/1520014-usesprecisecollisiondetection

i don't know if works, please let me know.


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -