Search Unity

Terrain and footsteps?

Discussion in 'Scripting' started by BlueRain01, Apr 6, 2012.

  1. BlueRain01

    BlueRain01

    Joined:
    Aug 22, 2010
    Posts:
    86
    Hi all .............

    i'm trying to make the effect of footstps , so when the player walk on wood for example play sound clips .
    for the mesh and like the props an building thats will be easy with ''OnControllerColliderHit'' function . so tag your collider and set your variable and it 'll work example :


    if (controller.isGrounded hit.gameObject.tag == "Wood") {
    audio.Play();
    }

    Allright. this just an example .

    What i wan to know if i can acces to the terain properties . for example if i use a sand texture around the terrain i want a footstep sound played whatever the player walk on that texture .
    is that possible ?
    thx..
     
    Last edited: Apr 6, 2012
  2. karljj1

    karljj1

    Joined:
    Feb 17, 2011
    Posts:
    440
  3. BlueRain01

    BlueRain01

    Joined:
    Aug 22, 2010
    Posts:
    86
    amm .. but how can this help if it return the color of the pixel ?
    can you explain more ?
     
  4. karljj1

    karljj1

    Joined:
    Feb 17, 2011
    Posts:
    440
    Terrains use splatmaps to texture themselves. Its basically a single texture where each colour represents a tiled texture. So when you paint on a grass texture on your terrain it is in fact painting a single colour onto a splat map which references your grass texture. So for example your splat map may use red for grass, green for sand, blue for rock etc.

    So if you can look up the splat map pixel colour you are standing on, you can then lookup that rgb value as a key and see what type of terrain it is.

    An example splatmap:



    Karl
     
    Last edited: Apr 6, 2012
  5. BlueRain01

    BlueRain01

    Joined:
    Aug 22, 2010
    Posts:
    86
    oh ...yes but how can i know or how i can use it .
    i mean where i should put this code on the terrain or in my footsteps script ? should i use it inside'' OnControllerColliderHit''?
    can you guide me with an example?
     
    Last edited: Apr 6, 2012
  6. karljj1

    karljj1

    Joined:
    Feb 17, 2011
    Posts:
    440
  7. BlueRain01

    BlueRain01

    Joined:
    Aug 22, 2010
    Posts:
    86
    thank you karl !
    cheking these links.
     
  8. karljj1

    karljj1

    Joined:
    Feb 17, 2011
    Posts:
    440
    I think you also need to set your texture as read/write to get the pixel.
     
  9. BlueRain01

    BlueRain01

    Joined:
    Aug 22, 2010
    Posts:
    86
    soory but still dont understand !
    so fire a ray down to the ground and get the pixel color and then what ? how i can make my condition ?
     
  10. karljj1

    karljj1

    Joined:
    Feb 17, 2011
    Posts:
    440
    You need to figure out what colours have been used by unity to represent each texture. Once you know then you can do a simple lookup.

    E.G

    if pixel colour is red then use grass sound
    else if pixel is green then use sand sound
    etc
     
  11. BlueRain01

    BlueRain01

    Joined:
    Aug 22, 2010
    Posts:
    86
    huha that's what i want to know ! so i have to do it manually ? is that any chance to determinate this automatically?
    i expect to use a lot of textures in my map !!
     
  12. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    I would use this on other types of floor objects. So if you walk over a wooden bridge, it would not be on the terrain map, but the sound would come from the bridge it's self.

    And yes, you have to do it manually.

    you can get the initial information from the splat map and create your own map for the terrain. It inst quite laid out like karljj1 suggests but it is close enough to get information from it. I would create a simple 512x512 array that measures the full terrain. you then pole the terrain's splat map to get the data that you need. You could do grass, sand, wood, rock metal or whatever. You just plot values into the array. Then just retrieve them from the placement of the character in the world.

    Then, if you raycast down and the object you hit is a terrain, you use that sound, if it is some other collider you just ahve it tagged for some type of sound. Like wood for a wooden bridge, or you could tag it specifically for certain sounds. Echoing halls or something like that.
     
  13. BlueRain01

    BlueRain01

    Joined:
    Aug 22, 2010
    Posts:
    86
    BigMisterB
    thanks for your replay and you suggestions.

    i don't worry about other types of floor objects it's easy to get the result as i sad before ''OnControllerColliderHit' can get the job done .
    btw, i have receive a suggestion from friend tell me to use colliders and place them over each texture and tag the colliders and then use my script :
    if (controller.isGrounded hit.gameObject.tag == "Wood") {
    audio.Play();
    }
    i think it will work but how this effect the performance ?? what do u think guys?
     
  14. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    no, because that would then affect your terrain.
     
    Last edited: Apr 6, 2012
  15. BlueRain01

    BlueRain01

    Joined:
    Aug 22, 2010
    Posts:
    86
    what do u mean that effect my terrain?
     
  16. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    If you put a collider over a river, and told the character when he walked on it, it was a river, he wouldnt be working on the river, he would be walking on the collider. your best bet is to get a texture or array and get the pixel or value from the array stating what it is according to X an Z position in the world.
     
  17. BlueRain01

    BlueRain01

    Joined:
    Aug 22, 2010
    Posts:
    86
    yes, i have this issue before with the water thx.
    i 'll try with this method Texture2D.GetPixel
    thank you guys for your time.
     
  18. karljj1

    karljj1

    Joined:
    Feb 17, 2011
    Posts:
    440
    The collider method you mention okba28mca would work if you made them triggers. You could set the sound on the on enter trigger function. However this could mean setting up a lot of triggers.