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 pickledzebra · Jun 11, 2010 at 07:40 PM · raycastall

RaycastAll - does rigidbody on parent object interfere with hit-responses of child colliders?

I'm using the following bit of code (nearly verbatim from scripting resource). I think I may not be fully understanding the RaycastAll function. I'll have 2-4 "stacked" colliders and the script will show that hits.length = 2,3, or 4. However, I will only ever get the "top" collider's name or tag using the print statement.

function Update () {

 var hits : RaycastHit[];
 hits = Physics.RaycastAll (camera.main.ScreenPointToRay(Input.mousePosition),Mathf.Infinity);

 for (var i=0;i<hits.length;i++)
 {
     var hit : RaycastHit = hits[i];
     print(hit.transform.gameObject.tag);
     var renderer = hit.collider.renderer;
         if (renderer)
         {
             renderer.material.color.r += 0.35;
             renderer.material.color.g += 0.35;
             renderer.material.color.b += 0.35;
         }
     }

}

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

3 Replies

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

Answer by Mike 3 · Jun 12, 2010 at 11:06 AM

As you've mentioned, adding a rigidbody to the parent sets hit.transform references to the object with the rigidbody

Fortunately, it doesn't change hit.collider - this means it'll work just as expected if you change to that

As to whether this should be the case, my guess is yes - transform gives you back the object you're usually interested in when you're responding to raycasts (i.e. the root object of the enemy, etc etc), and collider gives you back the specific collider

Edit:

http://unity3d.com/support/documentation/ScriptReference/Collision-transform.html

All documented, so not a bad guess (Though next time I check the docs sooner)

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 fherbst · Jun 12, 2010 at 05:17 PM 0
Share

Interesting - I thought about writing hit.collider in my post, but then thought it would surely be the same. But it makes sense, it is much easier to build one rigidbody with multiple colliders when the returned transform is the one wit the rigidbody.

avatar image
0

Answer by fherbst · Jun 11, 2010 at 09:52 PM

Maybe you could use

var output:string;
for(hit in hits)
{
output += hit.transform.gameObject.name + ", ";
}
print(output);

to see if it works.

It would be strange if the loop gets executed correct but not printed.

Comment
Add comment · Show 4 · 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 pickledzebra · Jun 11, 2010 at 11:33 PM 0
Share

I tried your test (thanks for the suggestion). If GO1 is my first (topmost or nearest to the camera) gameobject, I get "GO1,GO1,GO1", "GO1,GO1,GO1,GO1".

So, is this a parent/child issue? Somehow I keep grabbing the name of the parent - even though it's the child's collider being hit?

avatar image Mike 3 · Jun 11, 2010 at 11:52 PM 0
Share

Testing the script for myself, i get 1,2,3 for my objects, which are all parented to each other (3 parented to 2 parented to 1) - Not a clue what you're doing wrong :S

avatar image pickledzebra · Jun 12, 2010 at 01:12 AM 0
Share

$$anonymous$$ike, could you possible try adding a rigidbody to your "top" or "outermost" game object? I've stripped everything off my parent object. Children's names show up fine with the original script. Adding a rigidbody to the parent, however, results in the original problem. Not sure why this would be.

avatar image pickledzebra · Jun 12, 2010 at 01:16 AM 0
Share

yeah, stripped all scripts, bells and whistles off all objects. Isolating the "rigidbody" on/off the parent object seems to be the source. Should I pose this as a new question?

avatar image
0

Answer by danielx9 · Oct 21, 2017 at 08:30 PM

I had the same issue and turns out the child's rigidbody was not being simulated, didn't even had to change the code, I just turned simulated on in the game object's inspector and it worked.

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

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

1 Person is following this question.

avatar image

Related Questions

[C#] Physics.Raycast returns an array and can't access the length 2 Answers

RaycastAll2D Not Working at Distance from (0,0) 0 Answers

Raycastall not casting and return null array 0 Answers

Tiles painted at runtime + 2D RycastAll = problem! 0 Answers

Bullet penetration logic 4 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