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 Muhwu · Dec 27, 2013 at 06:42 PM · raycastmeshonclick

Detecting a mouse click on a custom 2D mesh?

I have a 2D game core where I create trails using custom meshes. I want to be able to detect a click on this customly created mesh. I tried adding a MeshCollider and used an example code I found:

     if(Input.GetMouseButtonDown(0))
     {
         var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;

         if(Physics.Raycast(ray, out hit))
             Debug.Log("Mouse Down Hit the following object: " + hit.collider.name);
         else
             Debug.Log ("Nothing was hit!");
     }

It seems to "sort of work". I get an occasional hit from this, but the result seems really random.

Basically all I need is to see if my trail has been clicked, but at this point I'd be happy to get some insight into how I can visualize the shape the raycaster tests against.

[Update] Ok, so I found 2 issues with this:

1) The normals were only correct for half the triangles. I changed the generation algorithm so now the normals are faced towards the camera. However, now it only works for the 1st quad being drawn which takes me to issue 2:

2) The mesh collider isn't being updated despite me calling mCollider.sharedMesh = mMesh; at the end of the update. I can update it manually by clicking any of the variables in the editor, such as IsConvex to on and off and it works splendidly after that. How do I do this refresh manually through code? This is my currect mesh construction code:

 void ConstructMesh()
 {
     mMesh.Clear();

     mMesh.vertices = vertices;
     mMesh.normals = normals;
     mMesh.triangles = triangles;
     mMesh.uv = uvs;

     mMesh.RecalculateNormals();
     mMesh.RecalculateBounds();
     mMesh.Optimize ();

     mCollider.sharedMesh = mMeshFilter.sharedMesh;
 }
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 Muhwu · Dec 27, 2013 at 07:34 PM

Ok, so I figured out what the problem was. Since the sharedMesh is the same one that is originally set, setting it again doesn't actually update the reference. What I needed to do here was to

     mCollider.sharedMesh = null;
     mCollider.sharedMesh = mMeshFilter.sharedMesh;

This refreshes it. It looks ugly, so if there is a "correct" way to do it, I'd appreciate getting to know it.

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

18 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

Related Questions

Raycast to align character on MeshCollider normals 1 Answer

Generate mesh from raycast positions, independent of rotations 0 Answers

Get Vertice of a Quad (two tris) via raycast? 0 Answers

Returning list of Triangles, Vertices or Points by raycasting through a mesh (iPhone) 1 Answer

Raycast crashing build on collision with mesh collider 3 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