• 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 T-E-M · Jan 25, 2017 at 01:13 PM · rigidbody2dcollider2draycastingonmousedownkinematic

2D Colliders stops working at random despite rigidbody, OnMouseDown not called

I did a one day project to clone the solitare game from Shenzhen I/O to prepare for making another game. However at random times cards cannot be picked up. I'd like to understand the root of the problem and how I can fix it or do better, not alternative ways of making a card game (as I intended to carry things over to more complex games with scrolling world spaces).

What I have are:

  • Objects with a 2D collider component, most are playing cards using the box 2d collider

  • Colliders are set as triggers

  • These objects have a RigidBody2D component with body type set to Kinematic

  • OnMouseDown/OnMouseDrag/OnMouseUp are used to drag and drop cards

  • transform.position is set to move cards during dragging, Z order (and sprite order) is adjusted as needed

  • transform.SetParent is called to attach cards together when laid partially on top of each other

What I'm experiencing is at random times (maybe 1 out of 200) a card will become stuck - it no longer fires the OnMouseDown method when clicked and debugging shows that ray casting doesn't see the collider. Inspection shows coordinates and parent/child values are correct and the problem can be "fixed" by disabling the object and then enabling it again while the game is still playing in the editor.

This seems entirely like the behavoir expected when moving static colliders which I see a lot of questions about, but I was under the impression that if I added a kinematic rigid body that I could then move the object by setting transform.position and the colliders would be properly updated. Ultimately I need to be able to click or determine what is under a point in world space reliably, with some objects having more complex shapes (circle or polygon colliders). No physics are wanted.

Comment
Add comment · Show 7
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 MelvMay ♦♦ · Jan 25, 2017 at 01:51 PM 0
Share

No physics are wanted. This seems entirely like the behavoir expected when moving static colliders which I see a lot of questions about, but I was under the impression that if I added a kinematic rigid body that I could then move the object by setting transform.position and the colliders would be properly updated.

If no physics are wanted then why are you using physics objects? $$anonymous$$oving a static collider just seems like being rebelious for no reason; it's static for a reason ... you don't intend to move it. There are no problems "expected" at all with Static colliders. Add a $$anonymous$$inematic body when you intend to move it; add a Static body (or no body) if you don't intend to move it ... simple. Why is it such a problem adding a $$anonymous$$inematic body? I only ask because it's done time and time again, not adding a Rigidbody component due to some misconception.

Here's the rule: You should never ever move colliders, full stop. If you want to move colliders then attach them to a body and move that. Even if you only want to use the collider for collision-detection and not response this is true. I can only presume this is what you mean by "No physics are wanted". Collision detection is obviously physics.

Also note that the On$$anonymous$$ouse, OnXXX are all UI things. They all use the Physics2DRaycaster (a UI class) which itself uses Physics2D.GetRayIntersectionNonAlloc (a 2D Physics method). If you want to perform your own and much more efficient tests then you can use the 2D physics intersection tests yourself such as the Physics2D.OverlapPointNonAlloc call.

avatar image T-E-M · Jan 25, 2017 at 02:25 PM 0
Share

If no physics are wanted then why are you using physics objects

If you know of a way to perform world space hit tests in Unity without reinventing the wheel please say so. As far as I can see, and if you know differently why don't you post it as an answer and get some rep, Unity's mouse handling for world space objects is riding on the physics system with no clear way to seperate the two.

$$anonymous$$oving a static collider just seems like being rebelious for no reason; it's static for a reason

Which is why I'm not moving a static collider. I stated in my post at least 3 times, including in the title that I was using rigid bodies. I've stated that I'm already aware that moving static colliders is bad and that I'm not doing it. I'm repositioning kinematic rigidbodies, which all documentation I've been able to get so far says is correct, and yet I'm still experiencing collider bugs.

As for perfor$$anonymous$$g my own intersection tests, well I already am (to deter$$anonymous$$e what I've dragged over). I didn't mention that explicitly but I did state clearly that I had performed manual ray casting when I tried to debug the issue and the colliders where still not visible - in other words doing my own ray casts will not help if the rigidbody/collider has decided to stop working. The ray casting is not the issue, it's the collider disappearing or being misplaced on whatever optimized tree Unity has going on behind the scenes.

Show more comments
avatar image MelvMay ♦♦ · Jan 25, 2017 at 02:52 PM 0
Share

btw: If there's any way, whether that be automated or not that you can duplicate this situation then please submit it as a bug.

avatar image T-E-M · Jan 25, 2017 at 05:49 PM 0
Share

When I've been able to encounter this bug and inspect it in the editor the colliders where where they where supposed to be (both in the live game tab using gizmos to visualize colliders and on the scene editor tab where I selected the individual collider). Of course those lines are debug lines, likely drawn using the public collider data and not whatever optimized cache Unity has behind the scenes.

The idea that the colliders might be misplaced due to my own code is also less likely due to the fact that toggling the game object (unchecking its active checkbox and then checking it again) instantly fixes the issue without any other changes to the objects properties. I've also compared things like the info section for the rigidbodies, to see if there was something unusual about the sleep state or other values but it was identical to working cards.

As for reproduction that's really what drove me to see if anyone could provide insight. The rarity (most I've had are two stuck cards in a single game and many games have none) of the bug appearing has made it extremely difficult to deter$$anonymous$$e if a given piece of code triggers it. If I remove or change anything the only way to test if it had any effect is to play a lot of games, over and over again, until I either encounter the bug or I am forced into 'assu$$anonymous$$g' that the bug is gone, which given that it's likely a race condition of some sort is a really bad assumption. It's just as likely that I've only made it harder to trigger which is not a solution.

At the moment I am still trying to make a $$anonymous$$imal scene, object and code wise, that I can trigger the bug with but have been unsuccessful so far, whether because I haven't introduced the cause of the bug or because I haven't played the scene enough to randomly stumble on it I don't know.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Edge colliders not moving with the attached object 0 Answers

set collision force after isKinematic disabled 0 Answers

(2D) Can't use colliders when I have a kinematic rigidbody? 1 Answer

On click, select game object -working with objects in scene but not instantiated objects 2 Answers

How do I make a rigidbody2d that doesn't do anything on collision? 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