Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by Darius0995 · Jan 07, 2019 at 10:20 PM · colliderprogrammingcollider2dontriggerenter

Best practice for checking which object has collided.

In terms of performance, what is the best approach for checking which object has interacted with a collider/trigger? For instance, picking up items.

Is it better to have one script attached to the player that checks for the explicitly stated tags of each individual item; and checks through them all via an else if statement. Or rather, would the more optimal approach for performance be having a script on each individual item that then checks for the "Player" tag, thus not running through slews of if statements?

Sorry if I'm missing something obvious here, 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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Cornelis-de-Jager · Jan 07, 2019 at 10:36 PM

I would say on the player. Also if would do an overarching tag for all the intractable items. For example there is a sword and a healing potion. I would tag them both as interact-able.

That way all you need to do is write code for the player:

 OnTriggerEnter (Collider collider) {
   if (collider.tag ==  "interactable"){
     // Do further things specific to the item
   }
 }

That way you don't have do go through a list of items, and you stop the execution of the collision check on however many X amount of items, which in itself would have been X times more computationally expensive.

Comment
Add comment · Show 1 · 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 unity_0fR_i33qsWBfkg · Jan 08, 2019 at 07:43 AM 1
Share

Use CompareTag method

avatar image
3

Answer by cdr9042 · Jan 08, 2019 at 05:01 AM

Cornelis's answer is correct, but when comparing tags, you should use gameObject.CompareTag https://docs.unity3d.com/ScriptReference/Component.CompareTag.html

Otherwise, you risk generating too much garbage in memory.


The best way is to check their LayerMask, it's numeric comparison, more efficient than string comparison.

 static LayerMask itemLayer = LayerMask.NameToLayer("LAYER_NAME")

  function OnCollisionEnter(collision : Collision)
  {
      if (collision.collider.gameObject.layer == itemLayer )
      {
           Debug.Log("Touched an item");
      }
  }


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 Darius0995 · Jan 08, 2019 at 05:37 AM 0
Share

Thanks for that info. Just for clarification, if you could, on this overarching tag/layer comparison method stated above. This couldn’t be used if each individual item claimed a unique quality I.e it’s first instance needing to be added to a list. Each item would need its own unique tag/layer in that circumstance, correct?

avatar image cdr9042 Darius0995 · Jan 08, 2019 at 07:48 AM 0
Share

You mean like checking if it's colliding against an unique object? Or only check colliding against first object and ignore the rest if it's colliding against multiple objects in one frame?

avatar image Darius0995 cdr9042 · Jan 08, 2019 at 01:24 PM 0
Share

The approach of having each item that can be picked up under the same tag. What if each item on pick up needed to call its own individual functionality, this overarching tag approach wouldn’t be possible right, or do I use another method to distinguish between what’s been picked up other than checking it’s tag?

Show more comments

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

137 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 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

Multiple Cars not working 1 Answer

OnTriggerEnter is running twice when collisions is detected. Couldnt fix. 1 Answer

How Does OnTriggerEnter() Work? 0 Answers

Problem with OnTriggerEnter 2 Answers

Using collider to gameobject detect attack or not 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