• 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
Question by Ballowii · Jan 02, 2018 at 06:32 PM · collisionrigidbodyraycastcollider

Raycast doesn't register Rigidbody

This exact code (except for the .rigidbody) worked wonderfull when i tried to hit box.colliders with it. Only thing I changed was turning the box.collider into a rigidbody and suddenly it doesn't react at all. Anybody any idea wtf is going on ?

   if (Physics.Raycast(transform.position, transform.up, out hit, lookingDistance)){ 
                                if (hit.rigidbody){ Debug.Log("Da");}
     }




Comment

People who like this

0 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 hexagonius · Jan 02, 2018 at 08:32 PM 0
Share

Just for the record, in Unity, you mustn't do this if (hit.rigidbody). There has been a lot of debates about it. The thing is Unity has overriden the way null is checked cause for every C# representation of things (rigidbody, collider) there's also a C++ component in the engine. This way of checking for null does not guarantee the C++ component exists. This might be only be true in cases where C++ straight removes objects from memory while the C# garbage collector kicks in some time later. So use if (hit.rigidbody != null)

If that doesn't fix it, the object you're raycasting against does not have a Rigidbody, or you're not even hitting what you're trying to hit.

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by Lysander · Jan 02, 2018 at 08:49 PM

A Rigidbody and a Collider are not swappable- they aren't the same thing, they just both have to do with the physics engine. A Rigidbody says "the physics engine should track or control this object, and allow it to interact with other physics objects", while a Collider says "this is the shell that serves as a physics obstacle". A moving object that needs to collide with other objects needs both. A static object that other objects collide with only needs to have a Collider (the other objects need a Rigidbody AND Collider to do the colliding).

A physics raycast doesn't hit Rigidbodies on their own (they have no 'shell' defined to hit), the "RaycastHit" objects they produce just has a shortcut property that's exactly the same as calling GetComponent() on the colliding object. If the hit objects has no Rigidbody, because it's stationary, hit.rigidbody will be null. If an object has a Rigidbody but no Colliders, it cannot collide with anything, and it cannot be hit by raycasts- this is likely only useful for flying objects that need to be affected by physics for visual reasons (like certain special affects), but which don't actually affect anything else in the scene.

Hope that helps!

Comment
Ballowii

People who like this

1 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 Ballowii · Jan 02, 2018 at 09:36 PM 0
Share

Thank you very much. I originally thought unity somehow blocked me from using a collider and a rigidbody on the same object, but it seems like I just imagined this cause now it worked like a charm. Sometimes I myself am my worst enemy I guess. Anyway, thanks a lot!

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

159 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

Related Questions

Audio.PlayOneSHot won't work? 0 Answers

Smooth Movement on Geometry Collission? 0 Answers

Raycast Not Drawing In Target Direction 0 Answers

Detect collision point?? 1 Answer

Rigidbody Wall Collision Bug/Wall Jitter 0 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