- Home /
How do I use collisions for player/enemy interaction
I went through the 2D Platformer Controller tutorial and wanted to do some expansion. I want to set up a simple enemy that can hurt the player and can be hurt by the player. I am currently using the collision code from that tutorial and want to know how to expand it to detect different tags. I'd rather not just use OnTriggerEnter code, as I am trying to learn new things. I know it is related to the contact filter, but I don't really know much about the subject. Any steps I can take?
Answer by EmreB05 · Nov 21, 2017 at 09:11 PM
I really recommend checking this tutorial series
https://www.youtube.com/watch?v=j48LtUkZRjU&list=PLPV2KyIb3jR53Jce9hP7G5xC4O9AgnOuL
While I did find this tutorial series interesting and informative, it does not answer my original question. I am good enough to use the unity collision functions, but I am trying to learn how to differentiate between two collided objects and execute different code for the different objects without using the OnCollision and OnTrigger functions. Should I be looking at an additional series from the same user?
Answer by Nikaas · Dec 23, 2017 at 07:28 AM
You can read about Layers (note that Sorting Layers are a diferent thing). Layers can be used to disable/enable collision detection between objects from specific layers. But still the standart way is to differentiate objects by some property inside the OnCollision and OnTrigger methods (and their 2D counterparts) because the number of Layers is limited. So use them wisely only when it really mattets (i. e. only to filter big amounts of unnecessary collisions/triggers).