I would like to achieve the classic 'headshot' effect in a sprite-kit game.
My ideas and attempts:
Create separate 'body' and 'head' physicsBodies on the same sprite and pin them with a joint. Impossible :(
Create a 'body' physics body on the "enemy" sprite and create another transparent sprite with a 'head' physicsBody that follows the body (sits on top). This approach requires me to keep track of what head is associated with what body. It will also decrease the games efficiency. I am curious if I could make a subclass of SKSpriteNode and create an "enemy" that has these two spritenodes and physicsBodies tied together.
Check the location of the projectile vs. the enemy to see if the projectile made contact in a certain Y range on the top of the sprite. I believe this will be inefficient as the projectiles are flying quite fast.
I would appreciate it if anyone has created this effect and can share their knowledge. Thank you!
Using the
pinned
attribute of a physicsBody makes it easy to create complex sprites with multiple contact zones. Pinning a Physics Body to the Node's Parent:Here is a basic Dog class, where the dog has a main body and a head. Note that I set
allowsRotation = false
as well so the head cannot move around the body.I use this setup in a lot of places. It scales pretty well if you find yourself wanting to add more parts, or weapons, and it lets you visually hide any of those parts or weapons easily. The use of userData can come in handy if you have a lot going on and different enemies have different "things", or you're doing crazy stuff like parenting child sprites to different entities based on gameplay, etc.