Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 brentshermana · Aug 11, 2015 at 04:17 PM · unity 5raycastnullreferenceexceptioncollider2draycasthit

RaycastHit2D.collider is null - why?

I've already looked at the docs, which say that 'collider' is only null when no collider is hit -- let me show you why I think that's impossible.

Here's the code (C#):

 public Transform closestSeenHuman() {
         float distanceToClosestSeen = visionRange;
         
         Transform closestSeen = null;
         
         foreach (Transform t in info.targets) {
             float dist = Vector2.Distance((Vector2)transform.position, (Vector2)t.position);
             if (dist < distanceToClosestSeen) {
                 float angle = Vector2.Angle (Utils.forward2D(transform, 'd'), t.position - transform.position);
                 if (angle < fovAngle * 0.5f) {

                                     //Here's the part that matters:
                     Vector2 direction = (Vector2) t.position - (Vector2) eyes.position;
                     RaycastHit2D hit = Physics2D.Raycast((Vector2)eyes.position, direction, Mathf.Infinity, ignoreEnemy.value);

                                     //this prints every frame that the human is in view
                     if (hit.collider == null) print("null collider");
                     
                     Debug.DrawLine(eyes.position, t.position);
                     
                     if (hit.collider.transform == t) {
                         distanceToClosestSeen = dist;
                         closestSeen = t;
                         print("seen");
                     }
                 }
             }
         }
         
         return closestSeen;
     }

The idea of the function is that if the human is within the scriptholder's vision parameters (both angle and distance) then a raycast is performed which, mathematically speaking, should hit the human or at least something in front of the human!

Here's my scene view: (turns out UA doesn't take images, so I posted a screenshot on Imgur) http://i.imgur.com/CyYAWdY.png

Visually speaking, the raycast has hit the human, and the human has a circle collider centered on its pivot point. What's going on?

Bonus points go to the brave soul who can explain to me why the Zombie's arms get in the way of its sight, even with the layermask.

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

1 Reply

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

Answer by brentshermana · Aug 11, 2015 at 09:25 PM

As it turns out I was using layer mask correctly: instead of selecting the layers that I did want to be found, I selected the ones that I didn't want to be detected. this actually solve both of my problems.

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

2 People are following this question.

avatar image avatar image

Related Questions

Click/Touch on Different part of sprite detected ? 0 Answers

Null Reference Exception issue getting the tag of an object hit by Raycast 2 Answers

Switch GameObjects Tags with javascript 1 Answer

Multiple raycasts from game object 0 Answers

Finding a RaycastHit tag (Null Reference Exception) 2 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