• 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 mquickel · Mar 04, 2015 at 01:49 PM · c#unityeditor

Need some help with Physics.Raycast hit

In the Unity UI, I created a new layer (index=10) and assigned it to a GameObject with a Mesh Collider. In my code, I am doing drawing a ray from the player to the enemy and rendering it for Debug purposes. I tried this in both Update and FixedUpdate. This seems to work okay, generally, but there are cases where the raycast hit is saying that there is a null collider when clearly I can see the ray passing through the collider.

This can be seen in the attached image where the aqua color is the collider mesh (rendering on) and the blue ray represents the cast. The code is below as well. It's driving me crazy! What can I do to remedy this? Thanks.

 ![var d1 = Vector3.Distance(v1, v2);
 var ray = new Ray(v1, v2);
 
 RaycastHit hit;
 Physics.Raycast(ray, out hit, d1, 1 << 10);
 
 Debug.DrawLine(v1, v2, new Color(0, 0, 255));
 
 if (Vector3.Distance(player.transform.position, transform.position) <= ChaseRadius  && hit.collider == null)][1]


[1]: /storage/temp/41796-capture.png

capture.png (62.7 kB)
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 sniper43 · Mar 04, 2015 at 03:01 PM 0
Share

I don't see the mesh collider being gizmo drawn, so check if the mesh is assigned to it.

Have you tried replacing the mesh collider with a box collider and seeing if it works then (this is better performance wise anyway)?

avatar image mquickel · Mar 04, 2015 at 03:07 PM 0
Share

Thanks - The collider is definitely there. I believe the issue is the rendering is drawing over top of the other gizmo. If I uncheck the collider at runtime, the enemy immediately comes running for me. Otherwise it's intermittent.

I did not try a box collider. Although it would work here there are obvious other spots in the level where this wouldn't work.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Navigate UI with gamepad 0 Answers

Cannot Send Class Object From One Scene to Another 1 Answer

Illuminating a 3D object's edges OnMouseOver (script in c#)? 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