- Home /
Checking for collisions without using strings
I've read somewhere that it's a good habit to only use strings for displaying actual text in Unity. I've just watched a dozen tutorials about interfaces and was hoping to try and detect collisions more elegantly and reduce dependencies on tags or gameObject names. However, the complexity involved in checking collisions between objects with varying behaviors is starting to become a bit too much for me. As an example, I managed to make the following work on an Entity base class from which many of my objects inherit (the player, enemies, powerups, obstacles...)
public virtual void OnCollisionEnter2D(Collision2D collision)
{
IKillable killableEntity = gameObject.GetComponent<IKillable>();
IPlayerWeapon playerWeapon = collision.gameObject.GetComponent<IPlayerWeapon>();
if (killableEntity != null && playerWeapon != null)
{
killableEntity.Kill();
}
}
This kills an entity if it's killable and if it collides with a player projectile weapon. However, if I try to do this with every object in the game it's going to turn into a big mess. Is there a more elegant way to check for collisions (either using interfaces or something else), knowing that some collisions need to trigger a completely different behavior (e.g. ICollectable) ?
Thanks.
Answer by N1warhead · Jan 26, 2018 at 05:12 PM
Honestly, you may not like this answer, but currently there really isn't anything better than using tags. Yeah I know, sucks, but running a GetComponent like you are right now is worse than running 200 tag checks at once (Figuratively speaking).
The only thing I can really say is, try to create a new type of system that gets rid of tags. That's one thing I've always loved about programming, you can always make things better, it's just finding the way to do it.
But using GetComponent isn't the way, that's taking 20 steps back.
Well I'm not into liking an answer or not :) I'm trying to learn as much as possible in a very short time. The thing is, I'm reading a lot about the bad habits of spaghetti code. Right now my first prototype game is functioning just like I'd like it to, but most components are interdependent (and it was even worse before I learned how to use delegates & events properly). So I'm trying to make the different elements as modular and reusable as possible, hoping that this will help further down the road, in more complex projects. $$anonymous$$aybe I'm trying too hard... I've tried moving the collision code between projectiles and entities to the projectile class, and this takes one less GetComponent check. But if you say that I'll have to go with tags then I will. It just seems very project-dependent and not very clean-looking.
void OnCollisionEnter2D(Collision2D collision)
{
I$$anonymous$$illable entity = collision.gameObject.GetComponent<I$$anonymous$$illable>();
if (entity != null) { entity.$$anonymous$$ill(); }
Destroy(gameObject);
}
(cf. See number 11 here: https://www.gamasutra.com/blogs/HermanTulleken/20160812/279100/50_Tips_and_Best_Practices_for_Unity_2016_Edition.php)
Thank you for the comments. I'll stick to tags until I learn something better :)
Thanks for the link: https://www.gamasutra.com/blogs/HermanTulleken/20160812/279100/50_Tips_and_Best_Practices_for_Unity_2016_Edition.php) Very nice
I mean I can see why you're fearing to use strings, they can be problems, just depends what you're trying to do.
For example, the image below (In a link to my twitter), was hundreds of megs of strings reading from an exe file, I took the data as strings and converted the data to pixels (RGBA)...
So I took hundreds of megs of strings, and every 4 strings I did an RGBA Conversion and then at the end of that, I would loop through millions of strings (each string containing (RGBA) Values), then I'd draw them onto an image.
All that - in less than a couple seconds. https://twitter.com/N1warhead/status/922952138950692864
Thats the link to the above image I was telling you about. (actually 3 images).
Now with that being said, where strings will really mess you up is via DEBUG.LOG, now that will kill your performance quicker than anything.
I agree with N1warhead. I don't think tags will be a problem. $$anonymous$$eep in $$anonymous$$d that Unity recommends that for best collision performance, you use layer masks. You can set those in the collision matrix of the editor. You can also reference a layer using an int if your so concerned about strings. If you really want to avoid tags, I guess you could probably expand the collider class to save an int into it, that you could use as a tag (?). Still I think that layers in combination with tags just make a lot of sense.
Your answer

Follow this Question
Related Questions
Collisions with CharacterController? 0 Answers
Collision problem Ignorecollision 2 Answers
Sequencing Spawning for Multiplayer 1 Answer
Allow one object to pass through another whilst keeping collisions. 1 Answer
Detecting a string of collisions 2 Answers