- Home /
First Person Controller Detect Texture
Hello, I'm new to Unity3D development and currently using the indie version. I have a basic scene with a terrain that has dirt, and grass textures, there is also a wooden bridge at one point. I'm looking to have step sounds played via the first person controller. I have a basic script that plays the sounds but I want to detect what kind of texture the player is on. What is the best way to go about doing this. Please, if you can, be as detailed as possible, I'm pretty new to this.
Thanks in advance!
All I can think of is Terrain.terrainData but I never tried working with it.
Answer by greatestprez · Nov 23, 2012 at 04:43 PM
Use trigger colliders. Example: pretend you have a straight dirt road that leads to a grassy meadow. you want to play a walking sound that matches the dirt road when you are on the road, and you want to play a walking sound that matches the grassy meadow when you are on the meadow. To accomplish this, you can create a an empty object containing a trigger collider that stretches the length of the road, and then do the same for the meadow. You can then apply a simple script to each object containing the trigger collider (the 2 empty objects you created before). The scripts would make use of the OnTriggerEnter() and OnTriggerExit() functions to detect when the player enters and exits each area. The two functions would look something like this:
void OnTriggerEnter(Collider col) { // this will be triggered when the player enters
if (col.gameObject.tag == "player") { // make sure your player has the tag "player"
// insert code to play sound here
}
}
void OnTriggerExit(Collider col) { // this will be triggered when the player exits
if (col.gameObject.tag == "player") { // again, make sure your player has the tag "player"
// insert code to stop sound here
}
}
Yes, that's one way of doing it but will require lots of different tirggers for a large map.
True, but I don't think there is a way to detect textures, so triggers are the best way to solve the problem in my opinion.
Answer by SuarescorX · Mar 03, 2014 at 12:05 AM
There is a nice way, but is unofficial :D
http://answers.unity3d.com/questions/34328/terrain-with-multiple-splat-textures-how-can-i-det.html