Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

footsteps

Discussion in 'Scripting' started by dacloo, Jun 28, 2007.

  1. dacloo

    dacloo

    Joined:
    Jun 30, 2005
    Posts:
    469
    Hi!

    What is the best way to check which material (or even better: texture) the player is walking on? I was thinking about a raycast downwards, but how can I return information about its material or texture?

    This makes it possible to trigger a sound like grass or rocks (footstep sounds).

    Thank you.
     
  2. Lka

    Lka

    Joined:
    Aug 6, 2006
    Posts:
    297
    You could add a tag to the object or, if you use tags for other things, create a list of tags for each sound or material.
     
  3. dacloo

    dacloo

    Joined:
    Jun 30, 2005
    Posts:
    469
    The problem is that one single mesh may contain several materials (e.g a metal platform with sand texture on the right, resulting in two different footstep sounds), so tags wouldn't be sufficient.
     
  4. Omar Rojo

    Omar Rojo

    Joined:
    Jan 4, 2007
    Posts:
    494
    Divide the mesh in a grid and associate every cell to an specific sound..

    If the mesh is too detailed.. i will be a problem so a more sofisticated solution will be needed, like triggers for area colliders or use coded textures in the RGBA channels to get the appropiate sound (like the grid but in the texture and the color will be your sound index).

    PD: Post a screen shot of your terrain..

    .ORG
     
  5. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    If you can combine the textures into one single texture, then there is an easy way to find which surface you are walking on. The RaycastHit class contains a property for the UV coordinate of the hit point. Map out your texture image so that each type of surface (sand, metal, etc) is contained in a specific rectangular area of the texture image. Then you just need to see which rectangle the hit point's UV falls within and you will know which surface is underfoot.
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Code (csharp):
    1. if (Physics.Raycast(transform.position, Vector3.up*-1, hit, 5)) {
    2.     var hitMaterial = hit.collider.renderer.sharedMaterial;
    3.     print ("Material: " + hitMaterial.name + "   Texture: " + hitMaterial.mainTexture.name);
    4. }
    5.  
    Though there might be other issues, as other people have mentioned, depending on what you're doing exactly.

    --Eric
     
  7. dacloo

    dacloo

    Joined:
    Jun 30, 2005
    Posts:
    469
    Thanks for all the tips guys! Wonderful.
    Eric5h5; thanks for the code!

    :D
     
  8. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Code (csharp):
    1. if (Physics.Raycast(transform.position, Vector3.up*-1, hit, 5)) {
    2.     var hitMaterial = hit.collider.renderer.material;
    3.     print ("Material: " + hitMaterial.name + "   Texture: " + hitMaterial.mainTexture.name);
    4. }
    5.  
    For making this really perfect, you want to use sharedMaterial in this case, because that will not instantiate the material so that it is unique for modification, since you are only reading the value.
     
  9. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Oops, right...I knew that. ;) Fixed code above....

    --Eric
     
  10. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    that's only going to get the main texture though. so you could only have 2 sounds (by a bool) - right? say you want 3 because you have dirt, grass and cement in a layered shader. painting vertex colors and checking those could work. you could have 4 sounds (rgba, one for each channel - kind of like what omar was getting at). more i suppose if you checked for mixed colors.

    though most likely i'd take the easy route and build some simple, non-rendered geometry with an OnTriggerStay script to play the clip.
     
  11. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It would have as many sounds as you want. It depends on each type of terrain being a different object with its own material though. But if you're not using blended textures, that seems like the easiest solution.

    --Eric