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
1
Question by RealMTG · Apr 09, 2015 at 10:03 PM · raycastmaterial

Getting material index from raycast

Hi!

First of all, I don't know if index is the correct word for this. If it isn't, please correct me! Now for the problem. I am working on a footstep system and I need to find the right material the raycast hits. My map is built by tiles using full meshes so floor, walls and roof are all connected. Thanks to this I can not use tags and if I do a simple "check for material, do that if that material equals that" because the materials are in an array. Unfortunately the wall material is in first place and the floor always' last. So for the the "check material" method to work I need to walk on walls. So how can I check what material I am getting when it is in an array?

This is my code so far.

 if (Physics.Raycast(groundCheckTransform.position, -Vector3.up, out groundHit, 0.4f, movementSettings.walkableLayer))
         {
             Material[] hitMaterial = hit.transform.GetComponent<Renderer>().materials;
         }

The code doesn't do much at all at this stage. It just gets the objects materials and puts it in a array.

Thanks in advance!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by MUGIK · Nov 26, 2021 at 05:52 PM

This worked for me:

         public static int GetSubMeshIndex(Mesh mesh, int triangleIndex)
         {
             if (mesh.isReadable == false)
             {
                 Debug.LogError("You need to mark model's mesh as Read/Write Enabled in Import Settings.", mesh);
                 return 0;
             }
 
             int triangleCounter = 0;
             for (int subMeshIndex = 0; subMeshIndex < mesh.subMeshCount; subMeshIndex++)
             {
                 var indexCount = mesh.GetSubMesh(subMeshIndex).indexCount;
                 triangleCounter += indexCount / 3;
                 if (triangleIndex < triangleCounter)
                 {
                     return subMeshIndex;
                 }
             }
 
             Debug.LogError(
                 $"Failed to find triangle with index {triangleIndex} in mesh '{mesh.name}'. Total triangle count: {triangleCounter}",
                 mesh);
             return 0;
         }


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

Answer by Cherno · Apr 09, 2015 at 10:11 PM

  1. Get triangle index of the raycast hit: RaycastHit.triangleIndex

  2. Get SubMeshCount

  3. Use Mesh.GetTriangles to iterate through all triangles until you find the right material (= submesh) index.

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 RealMTG · Apr 25, 2015 at 03:20 PM 1
Share

@Cherno I have been experimenting with this but I can't figure out how or what I am supposed to use sub$$anonymous$$eshCount and $$anonymous$$esh.GetTriangles for. Could you help me a bit with this?

avatar image the_mr_matt · Dec 19, 2015 at 11:16 PM 0
Share

Updated link for no. 2: http://docs.unity3d.com/ScriptReference/$$anonymous$$esh-sub$$anonymous$$eshCount.html

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

Is this an efficient way of changing a material by script? 0 Answers

Changing texture with raycast on iPad 2 Answers

Make a selection 0 Answers

Pull Color from pixel of a an invisible texture from Raycast. 1 Answer

access material that is linked to face 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