Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
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 which are plots for plants and the way it works is that Plot spawns a child GameObject which is a Plant.


Both parent (plot) and child (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 (child) 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 child to take the priority, and other solution that I've found was to change the position of Z to be higher than parent, but it doesn't seem to work at all.

The only other thing that I've been thinking 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[] hits;
         Ray ray = camera.ScreenPointToRay(Input.mousePosition);
         hits = Physics.RaycastAll(ray);
         
         foreach (RaycastHit hit in hits)
         {
             Debug.Log(hit.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.

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

Why do GameObjects need Rigidbodies to collide? 0 Answers

How to make different actions using OnMouseDrag with two different colliders on one Game Object. 0 Answers

Can a Platform effector work on a circle collider2D? 2 Answers

Make the bullet in a 2d Top Down shooter get destroyed after passing mouse position 2 Answers

Create an object when another one is clicked C# 1 Answer


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