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..
getpixel is what you need, should give you the rgb of the splatmap http://unity3d.com/support/documentation/ScriptReference/Texture2D.GetPixel.html
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
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?
You would do something like this: http://answers.unity3d.com/questions/163662/get-pixel-colour-on-a-ray-hit.html Fire the ray straight down. You could do it on your collider hit. More here http://answers.unity3d.com/questions/173216/get-color-pixel-from-terrain-with-a-wheelcollider.html
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 ?
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
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 !!
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.
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?
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.
yes, i have this issue before with the water thx. i 'll try with this method Texture2D.GetPixel thank you guys for your time.
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.