

How to detect terrain textures at a specific world position If, however you just want a working script that you can copy and paste, skip to the bottom of this post for my finished example. In this post I’ll show you how to detect what surface material the player is standing on step by step and how you can use that data to trigger footstep sounds.īelow I’ll explain how this works and show you the method I used to actually do this. This is ideal for determining what footstep sound should play. This can then be used to check the Alpha Map data at that position. It’s possible to detect what mix of textures the player is standing on at any given moment by converting the player’s current transform position to a coordinate on the alpha map image. In Unity Terrain textures are stored as Alpha Maps or “Splat Maps”, which are arrays of values that hold the mix of each different texture at every point on the Terrain. So, when you’re using terrain in Unity, how can you detect the terrain texture that the player is walking on? Terrain objects, like any other object, can only have one tag but they will often use multiple different surface textures that each require different sounds. Well, that’s exactly the problem I ran into while working on my Sun Temple video series. At first I though it would be simple, after all detecting a Game Object’s surface material is as easy as adding a tag, right? but what if you can’t use tags, what if you’re actually using terrain for some or all of the walkable surfaces in your game?



This flow of operations effectively allows my team’s PlayerMovement script to iterate as usual, but be fed different live values decided by the terrain type.Recently, I’ve been trying to find the best way to build a footsteps system in Unity. SetPlayerMovementVariables(rep, textureValues) Initialized with totalTerrainsįloat closestDistance = Vector3.Distance(terrainCenter, playerPos.value) will be closest distance between player a terrain. Vector3 terrainCenter = new Vector3( + / 2,, + / 2) closest terrain, Initialized with totalTerrains If (totalTerrains.Length = 0) return null Įlse if (totalTerrains.Length = 1) return totalTerrains Terrain totalTerrains = Terrain.activeTerrains
UNITY TERRAIN TEXTURES CODE
Here’s the code return the closest terrain to the player! Just remember I use Scriptable Objects to store live values (such as player posisiton), hence the need for me to specify “.value” to get the Vector3. The terrain returned is then interpolated on to determine what textures is beneath the player. This function takes the player’s position, and compares it to the center point of the terrains stored in the array Terrain.activeTerrains. The solution here for me was to setup a function to work in tandem with the terrain splat mapping calculations above. Where my specific need for this process differs from a lot of what I’ve seen online is that our Unity scene involves upwards of 20 separate terrain objects (and therefore over 20 individual TerrainData data type). While these calculations are somewhat daunting (involving the 3D float array used to represent an alpha map), they’re actually not too complex when broken down. The process itself is not complicated, it just involves getting the terrainData of terrain below the player, and using the player’s relative position to the terrain to calculate what the texture is that is present at that point on of the terrain, and in what strength. For example, we have a texture that represents “heavy snow”, which will bring down the speed at which the player treks through the snow, as well as an “ice” texture, which would instead speed the player up at a much higher rate, and slow down at a much slower rate. Something that is crucial to the design of our game is different textures on the terrain beneath the player having differing effects on gameplay. I want to take your mind off the madness for a moment and talk about something I worked on over the last few days.Īs you can read about in my October Update, I’ve been making a skiing game! It’s been awesome so far, and really enjoyable. The last week has been a truly fitting capstone on the crazy year that has been 2020, especially for U.S. Hello all, I hope that as you read this, all is well in your world.
