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
1
Question by Atarius · Feb 07, 2016 at 07:59 PM · raycastcolliderboxcollider

Raycast is not hitting a box collider?

My raycast from my camera is not picking up on a box collider I have in my scene. It works fine on a mesh collider, but not the box collider for some reason. Please help! Thanks!

RaycastHit hit;

     if (Input.GetKeyDown("mouse 0"))
     {

         Debug.Log("Smasher");
         
         if (!Physics.Raycast(camera.ScreenPointToRay(Input.mousePosition), out hit))

             return;

         MeshCollider meshCollider = hit.collider as MeshCollider;
         if (meshCollider == null || meshCollider.sharedMesh == null)
             return;
         else {
             Mesh mesh = meshCollider.sharedMesh;
             Vector3[] vertices = mesh.vertices;
             int[] triangles = mesh.triangles;
             Vector3 p0 = vertices[triangles[hit.triangleIndex * 3 + 0]];
             Vector3 p1 = vertices[triangles[hit.triangleIndex * 3 + 1]];
             Vector3 p2 = vertices[triangles[hit.triangleIndex * 3 + 2]];
             Transform hitTransform = hit.collider.transform;
             print(p0);
             print(p1);
             print(p2);
             p0 = hitTransform.TransformPoint(p0);
             p1 = hitTransform.TransformPoint(p1);
             p2 = hitTransform.TransformPoint(p2);
             Debug.DrawLine(p0, p1, Color.red);
             Debug.DrawLine(p1, p2, Color.red);
             Debug.DrawLine(p2, p0, Color.red);
             //print(Input.mousePosition);

         }
        
         BoxCollider boxCollider = hit.collider as BoxCollider;
         if (boxCollider == null)
             return;
         else {
             print("Box hit");
         }
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 tanoshimi · Feb 07, 2016 at 09:02 PM

That's because of..

 MeshCollider meshCollider = hit.collider as MeshCollider;
 if (meshCollider == null || meshCollider.sharedMesh == null)
          return;

...which broadly translates as "if the raycast hits anything other than a meshcollider, exit this function right now". It'll never get to the bit on line 34 where you test to see if it hits a box collider.

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 Atarius · Feb 09, 2016 at 08:34 PM 0
Share

Thank you so much for your reply! So I took that line of code out, and now I get this.

"NullReferenceException: Object reference not set to an instance of an object camControl.Update()"

when I click on something that is not a meshcollider but is a box collider

it points me to line 14 in the code i linked previously

$$anonymous$$esh mesh = meshCollider.shared$$anonymous$$esh;

avatar image Atarius Atarius · Feb 09, 2016 at 08:53 PM 0
Share

Never $$anonymous$$d, I got it! Thank you!

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

Player have to stop when hit a corner 0 Answers

Anyway to tell which side of a collider is hit? 4 Answers

How can I make diagonal collisions work with raycasts from a boxcollider? (3D, C#) 0 Answers

Colliders, Mesh or Box? 1 Answer

Rays: why 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