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 malkere · May 20, 2014 at 02:04 PM · raycastmeshmeshcollidertriangles

instanced mesh won't collide with raycast anymore

Hello.

I have a connect the dots game where you click three times (x,y) to generate a triangle with the vertices set as the three points of click. It generates the triangle fine and it used to collide fine. I ran into the problem (lots of problems) along the way of only one side being able to collide whether you clicked clockwise, or counter-wise, so now I have it setup to instance two completely separate meshes, one facing front, one facing back. Then I raycast from the "treasure" points straight out to see if they collide with the only collision possible objects, the meshes.

---It used to work. I rewrote everything so that I wasn't using just precalled variables, but now everything is lists so that eventually the player can level up and make more clicks/triangles/etc. Everything else works fine after conversion to lists, except for triangle collision.

---If I turn on the back wall's collider, all treasure points will hit it just fine; which means the raycasting and collision detection are working fine. for some reason it won't hit the meshes though.


???????????????

I can see their MeshRenderers just fine and they are applied the same mesh as the MeshCollider. It makes officially no sense to me anymore.

Please help.

note: the script below is from the main camera where the commands are given, and then from the mesh prefabs that are instanced and have their own set of vertices. I've since rewritten them to single lines/private/1,000 other ways. still not registering.??

     bool CalcHit(int num) {
         RaycastHit hit = new RaycastHit();
         Ray ray = new Ray();
         ray.origin = new Vector3(dots[num].transform.position.x, dots[num].transform.position.y, 50);
         ray.direction = new Vector3 (0, 0, -1);
         if (Physics.Raycast(ray, out hit, 51.0f)) return true;
         else return false;
     }

     public Vector3[] _vertices;
     public Vector3[] _normals;
     public Vector2[] _UV;
 
     public void SetTrianglePosition(Vector3 pos1, Vector3 pos2, Vector3 pos3){
         _vertices [0] = pos1;
         _vertices [1] = pos2;
         _vertices [2] = pos3;
         _UV [0] = new Vector2(pos1.x, pos1.y);
         _UV [1] = new Vector2(pos2.x, pos2.y);
         _UV [2] = new Vector2(pos3.x, pos3.y);
     }
     
     public void CreateTriangle() {
         Mesh mesh = new Mesh();
         GetComponent<MeshFilter>().mesh = mesh;
         GetComponent<MeshCollider>().sharedMesh = mesh;
         mesh.Clear();
         mesh.vertices = _vertices;
         mesh.uv = _UV;
         mesh.triangles = new int[] {0, 1, 2};
         mesh.normals = _vertices;
     }
Comment
Add comment · Show 1
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 stevethorne · May 20, 2014 at 02:18 PM 1
Share

try setting the shared$$anonymous$$esh of the collider to null first and then set the shared$$anonymous$$esh to the filter's mesh.

1 Reply

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

Answer by malkere · May 21, 2014 at 03:27 AM

In trying out stevethorne's idea I was moving lines around a bit. the prefab is already null, so I didn't get any results from adding a line to specifically Clear() or = null; the sharemesh, but changing the order of the code solved the problem:

     public void CreateTriangle() {
        Mesh mesh = new Mesh();
        mesh.Clear();
        mesh.vertices = _vertices;
        mesh.uv = _UV;
        mesh.triangles = new int[] {0, 1, 2};
        mesh.normals = _vertices;
        GetComponent<MeshFilter>().mesh = mesh;
        GetComponent<MeshCollider>().sharedMesh = mesh;
     }

it used to work in that order a couple weeks ago before I rewrote everything... so I'm not sure why it's gotten fussy about order, but logically I always thought it should be like this... had copied the basic script from a different answer on here and was using it for several weeks.

now it's applying the GameObject's MeshCollider.sharedMesh after the mesh's vertices are defined and it is hitting properly.....?? there seems to be no change with the MeshFilter.mesh, but I'm just glad it's fixed.

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

20 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 avatar image avatar image

Related Questions

Pointcast? - Raycast Point X from same Point X 0 Answers

RaycastHit Triangle Index Problem 0 Answers

Green Wireframe Length 1 Answer

How to Detect All Mesh Triangles Within a Given Area 2 Answers

How to find connected mesh triangles? 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