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 IceBeamGames · May 20, 2015 at 04:00 PM · c#raycasthit2dontriggerstayphysics2d.raycast

Physics2D.Raycast intermittently returning null

I am currently testing to see if the raycast from an AI hits the player. I'm triggering the cast using OnTriggerStay2D. When the other collider is the player object I peform a raycast to see if the AI can actually see them. The test seems to fail when the player is around 90 degrees perpendicular to the AI.

     bool checkInSight (Collider2D other, LayerMask mask)
     {
         // By default the threat is not in sight.
         bool rtn = false;
 
         if (threat != null && !threatHealth.dead)
         {
             Vector3 direction = other.transform.position - tr.position;
             float angle = Vector3.Angle(direction, tr.up);
 
             if (angle <= fovAngle)
             {
                 // Send out raycast towards player
                 RaycastHit2D hit = Physics2D.Raycast(tr.position, direction, fovRadius, mask);
                 Debug.DrawLine(tr.position, other.transform.position, Color.cyan);
 
                 // If the raycast isn't empty and hits the player...
                 if (hit.collider != null && hit.collider.gameObject == other.gameObject)
                 {
                     rtn = true;
                 }
 
                 if (hit.collider != null) Debug.Log("Ray Hit:" + hit.collider.gameObject);
             }
         }
 
         return rtn;
     }

I've debugged the code a bit and everything up to the Physics2D.Raycast call is correct when its failing. Initial impressions of the code probably makes the layer mask seem like a likely culprit, but because I'm getting hits 90% of the time, surely can't be causing it?

Comment
Add comment · Show 3
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 barbe63 · May 20, 2015 at 07:05 PM 0
Share

What this line reports to you when it fails?

 if (hit.collider != null) Debug.Log("Ray Hit:" + hit.collider.gameObject);
avatar image IceBeamGames · May 20, 2015 at 08:44 PM 0
Share

Nothing. If the raycast fails then the RaycastHit2D is completely null, so I get no print out.

avatar image IceBeamGames · May 21, 2015 at 09:18 AM 0
Share

Sorry, the hit collider is null, not the hit. The hit is completely empty.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by IceBeamGames · May 21, 2015 at 03:05 PM

Solved it!. The problem was the assumption (on my part) that other.transform.position would return the center of the collider, not the centre of the actual root GameObject. I forgot I've offset the collider and have the center point of the object at the player's feet.

Here is the fix:

     bool checkInSight (Collider2D other, LayerMask mask)
     {
         // By default the threat is not in sight.
         bool rtn = false;
 
         if (threat != null && !threatHealth.dead)
         {
             // Get collider local offset position and rotate it
             Vector3 direction = other.transform.rotation * other.offset;
             // Add to transform position and then minus current position to get direction
             direction = (other.transform.position + direction) - tr.position;
 
             float angle = Vector3.Angle(direction, tr.up);
 
             if (angle <= fovAngle)
             {
                 // Send out raycast towards player
                 RaycastHit2D hit = Physics2D.Raycast(tr.position, direction, fovRadius, mask);
                 // RaycastHit2D [] hit = Physics2D.RaycastAll(tr.position, direction, fovRadius, mask);
 
                 debugRays.Add(new DebugRay(tr.position, direction, Color.cyan));
 
                 // If the raycast isn't empty and hits the player...
                 if (hit.collider != null && hit.collider.gameObject == other.gameObject)
                 {
                     rtn = true;
                 }
 
                 if (hit.collider != null) Debug.Log("Ray Hit:" + hit.collider.gameObject);
             }
         }
 
         return rtn;
     }

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Calling a function from another script on Raycast2D 1 Answer

Check if Object is in a Position? (Using Compare tag) 1 Answer

RaycastHit2D does not work? 0 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