Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 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
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 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

· Add your reply
  • Sort: 
avatar image
1
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
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 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!

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

160 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

Related Questions

Camera Collision Not Working 0 Answers

How do I pass through certain colliders but detect all collisions? 1 Answer

Player Collision with objects (help) 3 Answers

Smooth rigidbody movement 2 Answers

Prevent player from walking through/pushing walls that are Rigidbodies and have Box colliders 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