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
0
Question by Dropye · Feb 17, 2017 at 10:20 PM · raycastmeshcolliderconvex

Raycast on Nonconvex MeshCollider

Hey there,

Iam am trying to use a Raycast for some color Picking.

First I Generate a Mesh with Mesh Collider and Mesh Renderer from Vertices.

     public Color colorBoring;
     public Color colorAngry;
     public Color colorHappy;
     Texture2D mainTexture;
 
     private Vector3[] Vertices = { new Vector3(-1,0,0), new Vector3(0,0.5f,0), new Vector3(0,1,0), new Vector3(1,0,0)};
     private int[] Indices = { 0, 1, 2, 0, 3, 1, 3, 2, 1 };
     private Vector2[] UVs = { new Vector2(0,0), new Vector2(0.5f,0.5f), new Vector2(0.5f,1), new Vector2(1,0)};
     private List<Color> colors;
 
     // Use this for initialization
     void Start () {
 
         colors = new List<Color>();
         colors.Add(colorHappy);
         colors.Add(Color.grey);
         colors.Add(colorAngry);
         colors.Add(colorBoring);
 
         Mesh mesh = new Mesh();
         mesh.name = "Picker";
         mesh.vertices = Vertices;
         mesh.triangles = Indices;
         mesh.uv = UVs;
 
         mainTexture = new Texture2D (128, 128);
         for (int i = 0; i < mainTexture.width; i++) {
             for (int j = 0; j < mainTexture.height; j++) {
                 Color pixelColor = Color.Lerp (colorHappy, colorBoring, (float)i / mainTexture.width);
                 pixelColor = Color.Lerp (pixelColor, colorAngry, (float)j / mainTexture.height);
                 mainTexture.SetPixel (i, j, pixelColor);
             }
         }
 
         mainTexture.Apply ();
 
         GetComponent<MeshRenderer> ().material.mainTexture = mainTexture;
 
         mesh.colors = colors.ToArray();
 
         GetComponent<MeshFilter>().mesh = mesh;
         GetComponent<MeshCollider> ().sharedMesh = mesh;
         mesh.RecalculateBounds ();
         mesh.RecalculateNormals ();
     }


This is simply a Colored Triangle.

Now the other Side the Raycast. According to the Manual I need a Nonconvex Mesh Collider to use textureCoord.

 if (triggerPressed) {
             Ray ray = new Ray (transform.position, transform.forward);
             RaycastHit hit;
             var raycast = Physics.Raycast (ray, out hit);
             Debug.DrawRay (transform.position, transform.forward, Color.red);
             if (hit.collider.gameObject == Picker) {
                 Texture2D map = (Texture2D)Picker.GetComponent<MeshRenderer> ().materials[0].mainTexture;
                 var pixelUV = hit.textureCoord;
                 Debug.Log (pixelUV + " " + hit.collider.gameObject.name);
                 pixelUV.x *= map.width;
                 pixelUV.y *= map.height;
 
                 Debug.Log ("x=" + pixelUV.x + ", y=" + pixelUV.y + " " + map.GetPixel ((int)pixelUV.x, (int)pixelUV.y));
                 //Debug.Log (hit.triangleIndex);
             } else {
                 Debug.Log (hit.collider.gameObject.name);
             }
         }    

But the Raycast Ignores the MeshCollider if it is Nonconvex. If I set it Convex, it will be recognised by the Ray, but then the textureCoord is {0,0}

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

Answer by Dropye · Feb 21, 2017 at 11:47 AM

Okay,

I just changed the Mesh and the Texture. Both are now Assets. Not what I wanted, but it works.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Why do I get a triangleIndex of -1 when I SphereCast a convex MeshCollider? 1 Answer

Get texture pixel color after raycast on rigidbody object 1 Answer

Convex MeshCollider Doesn't Work on 2D Plane 0 Answers

Building a prism (adding thickness) from non-convex mesh 0 Answers

Rigidbody and convex mesh collider 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