Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 ShadoX · Apr 27, 2021 at 03:19 PM · raycastcollider3d

Physics.Raycast passing through Colliders

I'm using the following code in Unity 2020.3.3f1 to detect clicked objects, but I noticed that spamming mouse button sometimes results in the Raycast just passing through a "solid" collider that it should detect.

 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
 if (Physics.Raycast(ray, out hit, 100.0f)){
     // ... 
 }

I already checked online and here on Unity Answers, but none of the mentioned answers or suggestions help and it seems to happen randomly while spamming mouse clicks. There's no pattern to it as to why it might happen and all clicked colliders are exactly the same. None are on a wrong layer, too far away or anything like that.

The Ray just seemingly starts passing through a random Collider until I've clicked on a different one (which it detects), after which the Ray will detect the Collider that had issues previously.. :|

Comment
Add comment · Show 1
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 ShadoX · Apr 27, 2021 at 03:25 PM 0
Share

Only thing I can think of is that I'm changing semi-rapidly the position of those objects via transform.position = ... but visually the GameObjects that end up having issues seem to be correctly placed including the Collider (I think?)

So I don't think that the Collider should be "magically" missing or not have moved with the GameObject ?

1 Reply

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

Answer by Bunny83 · Apr 27, 2021 at 06:51 PM

It's not clear what you exactly mean by "passing through". A Raycast is not a ray that somehow travels through space :). It directly tests for intersections with colliders. One thing you just mentioned in the comment but not in your question is that you seem to be moving colliders around. Do those colliders have a kinematic rigidbody on them? If not you should not move them at all.


Static colliders must not be moved at all. Colliders that do not belong to a rigidbody (kinematic or non-kinematic) are considered to be part of the world and should never move, rotate or scale. Doing so can have quite a performance hit as the world collider is recreated each time. So any moving / rotating / changing collider should be part of a rigidbody. Kinematic rigidbodies are meant to be controlled by scripts. So it's "safe" to just move their transforms around.


Also keep in mind that modifying the Transform of a collider / rigidbody does not immediately affect the collider representation on the physics side. If you want to raycast immediately after a change you have to call Physics.SyncTransforms().

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 ShadoX · Jun 05, 2021 at 03:29 PM 0
Share

Thanks for pointing it out :)

Forgot to accept the answer earlier, but I guess that was the issue. Once I added a Kinematic Rigidbody the issue seemed to disappear. At least I couldn't reproduce it manually, though I didn't get around to write some tests to run a few times "just in case" hah

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

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

Just wondering how I can create a child for each generated hex and make that child a sphere collider? Thanks. 2 Answers

Raycast and colliders problem. Corners of two colliders 0 Answers

How to rotate an object 360 degrees around a sphere with collisions? 0 Answers

Using raycast and collider to increase int 1 Answer

Cancel Input if the direction has a wall in the way 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