• 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
0
Question by Hakej37 · Nov 18, 2020 at 06:03 PM · unity 2dcollider2donmousedown

Overlapping BoxCollider2D (as triggers) not working correctly OnMouseDown

I've made a simple 2D top-down farming game (like Farmville) and I've made a couple of GameObjects with prefabBrush w$$anonymous$$ch are plots for plants and the way it works is that Plot spawns a c$$anonymous$$ld GameObject w$$anonymous$$ch is a Plant.


Both parent (plot) and c$$anonymous$$ld (plant) have their own BoxCollision2D with IsTrigger on true, and the problem is that either first or the second collision works when I click on it (i.e. raises the OnMouseDown event in specific script), where I would like so that both would work at the same time, because I can water the plot (parent) AND use scythe to destroy growing weed (c$$anonymous$$ld) on such plot for example. Because they are in the same locations, their colliders usually overlap almost completely.


I've read a lot about solving such issue and people recommend using RayTracing for mouse capture in such cases, yet none of them show how to implement it properly step by step, so it would imitate OnMouseDown as close as possible, like should I trace it in some static GameHandler script? Because tracing it in every single script is not only ugly, but also seems very unefficient. How do I raise some kind of event from GameHandler script to clicked objects' scripts then?


At least I would prefer for c$$anonymous$$ld to take the priority, and other solution that I've found was to change the position of Z to be $$anonymous$$gher than parent, but it doesn't seem to work at all.

The only other t$$anonymous$$ng that I've been t$$anonymous$$nking about was to use Composite Colliders and completely rearrange how the current plot/plant structure works, but I guess it would still behave inconsistently with adjacent plots and plants.

Any ideas?

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
0

Answer by xxmariofer · Nov 18, 2020 at 06:31 PM

Hello, For 2d objects dont use the Z, use the order in layer property a raycast example would be:

 void YourMethod()
 {
         RaycastHit[] $$anonymous$$ts;
         Ray ray = camera.ScreenPointToRay(Input.mousePosition);
         $$anonymous$$ts = Physics.RaycastAll(ray);
         
         foreach (RaycastHit $$anonymous$$t in $$anonymous$$ts)
         {
             Debug.Log($$anonymous$$t.gameObject.name);
         }
 }

if you are afraid about its performance limit the ray lenght and play with the collision matrix to avoid unnecesary ray collisions

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

148 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Block Game Object interaction with another Game Object with kinematic active 0 Answers

inconstant ground check on two identical platforms 1 Answer

Unity 2D character can slightly go through wall. 0 Answers

How to check if a polygon fits in specific 2D spot? 0 Answers

How is Rigidbody 2D Auto Mass calculated? 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