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 SHEePYTaGGeRNeP · Oct 24, 2017 at 08:51 AM · physicsraycastraycastinglayers

Some raycasts don't hit the object

Hello,

Some of my raycasts are not hitting the object. Raycasts

The purple lines are the one i Debug.DrawLine when I shoot the raycasts (doesn't happen every frame). The white and red lines are the whether the raycasts detected anything and that happens in the DrawGizmos.

In the image below you can see that distances in the inspector Raycasts distances

My question is: How is this possible? The Green car has only 1 collider and the code for each raycast is the same.

 Utils.FovAction(this.transform.up, this.transform.forward,
     this.nrOfRays, this.fov, (direction, index) =>
     {
         Ray r = new Ray(this.transform.position, direction);
         r.DebugDrawRay(Color.magenta, this.detectionRate, this.viewDistance);
         this._rayHits = Physics.RaycastAll(r, this.viewDistance, this.layerMask,
             this._queryTriggerInteraction);
         bool collided = false;
         foreach (RaycastHit hit in this._rayHits)
         {
             if (hit.collider.transform.IsTransformInMyParents(this.bot.transform))
                 continue;
             this.RaycastDistances[index] = hit.distance;
             collided = true;
             break;
         }
         if (!collided)
             this.RaycastDistances[index] = -1;
     });

When I debug I see that the _rayHits is empty.

I've also tried removing the Rigidbody on the green car, but that didn't solve it :(.

Comment
Add comment · Show 6
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 jchester07 · Oct 24, 2017 at 09:09 AM 0
Share

Is it always on the edge part of the collider that is not hitting? I heard raycast has some kind of offset. I'm not sure though.

avatar image SHEePYTaGGeRNeP jchester07 · Oct 24, 2017 at 11:23 AM 0
Share

No sometimes it's when it's like 10 range from it too in the center.

avatar image unit_nick · Oct 24, 2017 at 09:57 AM 0
Share
  1. Where is the problem? RaycastAll returns all the information for each Ray even if it has not hit anything. Perhaps your problem is the variable name _rayHits which is confusing you.

  2. Using RaycastNonAlloc is considerably faster than the other raycast methods for multiple raycasts.

  3. If you draw the Lines in the order of Raycast, _rayHits.transform != null you can simply draw over the top of the debug line if it hits which I expect would also look a lot less confusing.

avatar image SHEePYTaGGeRNeP unit_nick · Oct 24, 2017 at 11:29 AM 0
Share
  1. I know it does not return anything if it didn't hit anything, but it should hit something?

  2. I'll try that.

  3. I added the magenta lines for this post. Normally it only draws Gizmo lines.

Let's hope NonAlloc solves this somehow ;)

avatar image unit_nick SHEePYTaGGeRNeP · Oct 24, 2017 at 11:37 AM 0
Share
  • You are using a layer mask. If it doesn't find anything on the right layers it won't hit. You have even drawn the white lines showing the instances where it hasn't hit anything.

  • I don't think RaycastNonAlloc will solve any of your problems regarding hits or anything. It's just faster. As it looks like you're probably firing 11 rays each time you know exactly what size it needs to be.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

147 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

Related Questions

How do I use layermasks? 9 Answers

Raycast stop at distance 3 Answers

When i bring my mouse on layer mask my player doesn't move 0 Answers

Strange 'Glitch' while trying to apply the built-in layer "Ignore Raycast" via code 0 Answers

why Physics.Raycast is not working 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