- Home /
Collider detection in 2D
I am making a top down 2D game. There is a player, enemies, and enemies projectiles. The player can not move out of the playing area (which is a 1:1 square, the height of the camera). The enemies spawn off screen (at one of 12 predesignated spawning points) and move on screen. The enemies move in one direction and need to have distinctions between the head and body so if the player runs in to the body then they kill the enemy, and gain a point, if the player gets hit by the enemies head they get eaten and lose a point. The enemies also have projectiles, which spawn at the head, and travel in the same direction as the enemy, at a faster speed. This projectile kills the player, and they lose a point.
The problem is I don't know how to set up the colliders for this. I want the enemies and player to have a edge collider so the collision can more accurately represent the visual shape of the enemy. A capsule collider is almost the exact shape of the enemy projectile so I would like to use this. I also want a collider off screen so the enemies, and projectiles will collide with it and get removed from the game, in case the player does not kill it before it reaches the edge of the playable area.
My question is how do I make the colliders fit my use case, and how do I detect collisions threw scripts, so I can delete an instance of an object and add or subtract points from the score board.
(sorry if this is not very clear I don't really know how to explain this properly, if you need any other info please ask)
Thanks in advanced.
Answer by Larcondos · Apr 08, 2019 at 04:36 PM
Making my best attempt here, since this is a 2D game Unity actually has specific 2D Colliders Try using some of these within your game. I also have made a 2D top down game that sounds somewhat similar I could send you if you think it would help.
@Larcondos I was aware of the 2D colliders I just don't know how to utilise them properly. As far as I can tell edge colliders will not collide with other edge colliders, so I could make all the objects that need to pass threw the players boundary out of edge colliders, but then I would need two of the same colliders on the enemies, one for the head and one for the body, and I don't know how to distinguish between two of the same collider on one object in a scripts.
looking at how you did it may be helpful if you could send me that game.
Your answer
