• 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 Tyrdle · Oct 01, 2019 at 01:16 AM · rigidbody2dphysics2dcollider2dparentparent-child

Detect onMouseDown() on children colliders of a rigidbody parent

So I have been having trouble with detecting OnMouseDown() on the child object when parented to a rigidbody. I found out that rigidbodies make a combined collider with all it's children so when clicking, it detects the empty game object and not the individual child objects. Is there a way to detect which child collider was hit when hitting the rigidbodies combined collider? Is there a way around this?

Ignore Raycast layer does not change anything and I have tried using a manual ray from screen etc. I have tried hit.collider.name.

I need to have it setup this way so the parent rigidbody will be affected by physics whilst the children collide with the level and move altogether since they are children. I also need to do this inside 1 script.

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
0
Best Answer

Answer by Tyrdle · Oct 01, 2019 at 03:22 AM

So I have found a way to accomplish what I need, instead of parenting the children I just give them a fixed joint that is connected to the empty :) https://cdn.discordapp.com/attachments/556731187410894872/628430817290616832/unknown.png

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

Answer by starrtennis · Oct 01, 2019 at 01:34 AM

Wait, so you're saying that a RigidBody on a parent object yields OnMouseDown() events detected by the child even if the child doesn't have a RigidBody? That sounds ludicrous. Or do both the parent and the child have a rigidbody? Why do they need to be parent/children in the first place?

I would imagine the OnCollision from the RigidBody would just detect the parent OR the child. But you're saying they're overlapping in space? Then it would only make sense that both of them are detecting collisions. If you want both to have rigid bodies but only one detect collisions, you could set one as a trigger (the one you don't want to collide)--it will then only detect collisions but not act as physics, when a collider ENTERS it, if that makes sense.

Your description is a little ambiguous so if you could maybe explain with some code examples that might help.

Cheers, Mike

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 Tyrdle · Oct 01, 2019 at 01:47 AM 0
Share

Yes apparently rigidbodies will make a combined collider that includes all its childrens.

I have tried many combinations of colliders and rigidbodies and character controllers.

Essentially I am trying to have these 4 squares that makeup 1 big square (2x2) that move in unison, so I attached them all to a parented empty, the children collide and the rigidbody parent applys physics to them all, but clicking on them fires in the parent not the child clicked.

For example

 private void On$$anonymous$$ouseDown()
 {
     Debug.Log(gameObject.name);
 }


will just log "Player" the name of my parent empty object and not "Cube" etc. for the child objects.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

119 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

Related Questions

How to check place on empty for placement polygon Rigidbody2d? 0 Answers

How do i stop rigidbody2D bounce 1 Answer

Is this a correct way to have multiple different compound colliders in one gameobject? 1 Answer

Adding Collider2D changed physics on race car.How to compensate for mass loss in the back of the car?or do it some other way. 0 Answers

AddForce to RigidBody2D on the same frame as SetParent(null) causes small jitter 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