Hey guys, I’m trying to use the script in this link to add footstep sounds dependant on the terrain texture however it’s not working with the new Unity terrain in 2019.3, it just returns the first texture.
Any idea what is wrong? Thanks
Hey guys, I’m trying to use the script in this link to add footstep sounds dependant on the terrain texture however it’s not working with the new Unity terrain in 2019.3, it just returns the first texture.
Any idea what is wrong? Thanks
TerrainDetector:
using UnityEngine;
public class TerrainDetector
{
private TerrainData terrainData;
private int alphamapWidth;
private int alphamapHeight;
private float[,,] splatmapData;
private int numTextures;
public TerrainDetector()
{
terrainData = Terrain.activeTerrain.terrainData;
alphamapWidth = terrainData.alphamapWidth;
alphamapHeight = terrainData.alphamapHeight;
splatmapData = terrainData.GetAlphamaps(0, 0, alphamapWidth, alphamapHeight);
numTextures = splatmapData.Length / (alphamapWidth * alphamapHeight);
}
private Vector3 ConvertToSplatMapCoordinate(Vector3 worldPosition)
{
Vector3 splatPosition = new Vector3();
Terrain ter = Terrain.activeTerrain;
Vector3 terPosition = ter.transform.position;
splatPosition.x = ((worldPosition.x - terPosition.x) / ter.terrainData.size.x) * ter.terrainData.alphamapWidth;
splatPosition.z = ((worldPosition.z - terPosition.z) / ter.terrainData.size.z) * ter.terrainData.alphamapHeight;
return splatPosition;
}
public int GetActiveTerrainTextureIdx(Vector3 position)
{
Vector3 terrainCord = ConvertToSplatMapCoordinate(position);
int activeTerrainIndex = 0;
float largestOpacity = 0f;
for (int i = 0; i < numTextures; i++)
{
if (largestOpacity < splatmapData[(int)terrainCord.z, (int)terrainCord.x, i])
{
activeTerrainIndex = i;
largestOpacity = splatmapData[(int)terrainCord.z, (int)terrainCord.x, i];
}
}
return activeTerrainIndex;
}
}
and FootSteps:
using UnityEngine;
[RequireComponent(typeof(AudioSource))]
public class FootSteps : MonoBehaviour
{
[SerializeField] private AudioClip[] stoneClips;
[SerializeField] private AudioClip[] grassClips;
private AudioSource audioSource;
private TerrainDetector terrainDetector;
private void Awake()
{
audioSource = GetComponent<AudioSource>();
terrainDetector = new TerrainDetector();
}
//Step is an event from the Animation itself, everytime the animation fires "Step" , a Clip gets played
private void Step()
{
AudioClip clip = GetRandomClip();
audioSource.PlayOneShot(clip);
}
private AudioClip GetRandomClip()
{
int terrainTextureIndex = terrainDetector.GetActiveTerrainTextureIdx(transform.position);
switch (terrainTextureIndex)
{
case 0:
return stoneClips.Length > 0 ? stoneClips[Random.Range(0, stoneClips.Length)] : null;
case 1:
default:
return grassClips.Length > 0 ? grassClips[Random.Range(0, grassClips.Length)] : null;
}
}
}
Be aware that “Step()” is an event from my Animation, you can write a simple function by yourself to call whatever sound when ![]()
Thanks
and then the FootStep script would be added to your player object and the TerrainDetector script would be added to the terrain object?