Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Cirrocumulus · Jan 26, 2018 at 03:57 PM · collisionsinterfaces

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cirrocumulus · Jan 26, 2018 at 05:18 PM 0
Share

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)

avatar image Cirrocumulus Cirrocumulus · Jan 27, 2018 at 01:44 PM 0
Share

Thank you for the comments. I'll stick to tags until I learn something better :)

avatar image gwnguy Cirrocumulus · Jan 28, 2018 at 06:22 AM 0
Share

Thanks for the link: https://www.gamasutra.com/blogs/HermanTulleken/20160812/279100/50_Tips_and_Best_Practices_for_Unity_2016_Edition.php) Very nice

avatar image N1warhead · Jan 26, 2018 at 07:18 PM 1
Share

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.

avatar image calpolican N1warhead · Jan 26, 2018 at 09:17 PM 1
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

76 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges