1. We've introduced thread tags, search within a thread and similar thread search. Read more here.
    Dismiss Notice
  2. Learn how you'll soon be able to publish your games to China in four simple steps with Xiaomi. Sign up now for early access.
    Dismiss Notice
  3. Get further faster with the Unity Plus Accelerator Pack, free for new Unity Plus subscribers for a limited time. Click here for more details.
    Dismiss Notice
  4. We've released our first Timeline Experimental Preview, our new tool for creating cutscenes and more! To check it out click here.
    Dismiss Notice
  5. Unity 5.5 is now released.
    Dismiss Notice
  6. Check out all the fixes for 5.5 in patch releases 1 & 2.
    Dismiss Notice
  7. Unity 5.6 beta is now available for download.
    Dismiss Notice

Terrain and footsteps?

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

  1. okba28mca

    okba28mca

    Joined:
    Aug 22, 2010
    Posts:
    61
    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:
    441
  3. okba28mca

    okba28mca

    Joined:
    Aug 22, 2010
    Posts:
    61
    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:
    441
    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:

    [​IMG]

    Karl
     
    Last edited: Apr 6, 2012
  5. okba28mca

    okba28mca

    Joined:
    Aug 22, 2010
    Posts:
    61
    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:
    441
  7. okba28mca

    okba28mca

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

    karljj1

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

    okba28mca

    Joined:
    Aug 22, 2010
    Posts:
    61
    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:
    441
    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. okba28mca

    okba28mca

    Joined:
    Aug 22, 2010
    Posts:
    61
    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,075
    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. okba28mca

    okba28mca

    Joined:
    Aug 22, 2010
    Posts:
    61
    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,075
    no, because that would then affect your terrain.
     
    Last edited: Apr 6, 2012
  15. okba28mca

    okba28mca

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

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,075
    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. okba28mca

    okba28mca

    Joined:
    Aug 22, 2010
    Posts:
    61
    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:
    441
    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.