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
1
Question by hoodlma · Sep 09, 2013 at 08:58 PM · colliderraycollider.raycast

Collider.Raycast is complaining that my ray does not have a normalized direction

I have a bit of code that looks like this. I am investigating behavior when raycasts originate inside colliders, so assume that "point" may be inside the collider.

 public static bool foo (Vector3 point, Collider collider) {
     Vector3 vectorToCollider = collider.ClosestPointOnBounds(point) - point;
     vectorToCollider.Normalize();
     Ray rayToCollider = new Ray(point, vectorToCollider);
     RaycastHit dummyHit = new RaycastHit();
     if (!collider.Raycast(rayToCollider, out dummyHit, float.PositiveInfinity)) {
         return false;
     } else {
         return true;
     }
 }

I am running into problems because the actual collider.Raycast call is causing this method to break, because apparently vectorToCollider is not normalized. Specifically, I'm seeing:

 !isNormalized(ray.GetDirection ())

I'm very confused, since I believe I am correctly normalizing vectorToCollider before passing it in as an argument to the Ray constructor. I've also tried using vectorToCollider.normalized instead, but I've experienced the same problem.

Am I making a mistake, or am I running into a weird edge case with raycasts ?

Comment
Add comment · Show 2
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 getyour411 · Sep 09, 2013 at 09:41 PM 0
Share

Having just struggled with some Raycasting of my own, let me ask - is your use case simplified if you can tell your Raycast to ignore colliders on layer 1,2,3 and only report a hit on layer 4,5 (where the layers are defined next to the "Tag" in the editor, these are just examples)

avatar image hoodlma · Sep 09, 2013 at 10:22 PM 0
Share

is your use case simplified if you can tell your Raycast to ignore colliders on layer 1,2,3 and only report a hit on layer 4,5 (where the layers are defined next to the "Tag" in the editor, these are just examples)

I am developing a medical ultrasound simulation which will require very precise collision detection on a large number of meshes. The human model I am using (http://lifesciencedb.jp/bp3d/) has several hundred discrete organs, and I would like to be able to perform raycasts on individual organs. This is because different types of tissues have different characteristic textures in ultrasound images.

Worst case, it's certainly an option to group organs of similar tissue composition into the same layer and use the same texture for all of them, if collider.raycast isn't going to work for my purposes.

1 Reply

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

Answer by aldonaletto · Sep 10, 2013 at 12:49 AM

ClosestPointOnBounds(pos) returns the closest point when pos is outside the collider. If inside the collider, it returns pos. This results in a zero length vector, which can't be normalized - that's the origin of the mysterious error. Anyway, colliders are single faced: a raycast from the inside doesn't detect the collider.

EDITED: As @hoodlma pointed out, ClosestPointOnBounds(pos) returns pos when this point is inside the collider's bounding box - it doesn't matter whether it's inside the actual collider or not.

Comment
Add comment · Show 2 · 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 hoodlma · Sep 10, 2013 at 01:17 AM 0
Share

Thanks - this is what's going on. One correction though - ClosestPointOnBounds(pos) returns pos if pos is inside the Bounding Box of the collider, not necessarily inside the collider. So, for example, checking the inside of a hollow tube will result in this behavior.

avatar image aldonaletto · Sep 10, 2013 at 02:33 AM 0
Share

Good point! I tested this with a cube without any rotation, the only case where a collider is equal to its bounding box! Should have used any other collider shape, or at least the cube should be rotated.

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

16 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

Related Questions

RaycastHit.transform/collider 1 Answer

If ray in contact with collider... 1 Answer

collider.Raycast problem 2 Answers

Is there a way to detect if an object is being hit by a raycast? 2 Answers

Raycasting ignoring small sized object 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