Guest User

Footsteps

a guest
May 12th, 2020
161
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class CheckTerrainTexture : MonoBehaviour {
  6.  
  7. public Transform playerTransform;
  8. public Terrain t;
  9.  
  10. public int posX;
  11. public int posZ;
  12. public float[] textureValues;
  13.  
  14. public AudioSource source;
  15.  
  16. public AudioClip[] stoneClips;
  17. public AudioClip[] dirtClips;
  18. public AudioClip[] sandClips;
  19. public AudioClip[] grassClips;
  20.  
  21. AudioClip previousClip;
  22.  
  23. void Start ()
  24.   {
  25.     t = Terrain.activeTerrain;
  26.     playerTransform = gameObject.transform;
  27.   }
  28.  
  29. void Update()
  30.   {
  31.     // For better performance, move this out of update
  32.     // and only call it when you need a footstep.
  33.     GetTerrainTexture();
  34.   }
  35.  
  36. public void GetTerrainTexture()
  37.   {
  38.     ConvertPosition(playerTransform.position);
  39.     CheckTexture();
  40.   }
  41.  
  42. void ConvertPosition(Vector3 playerPosition)
  43.   {
  44.     Vector3 terrainPosition = playerPosition - t.transform.position;
  45.  
  46.     Vector3 mapPosition = new Vector3
  47.     (terrainPosition.x / t.terrainData.size.x, 0,
  48.     terrainPosition.z / t.terrainData.size.z);
  49.  
  50.     float xCoord = mapPosition.x * t.terrainData.alphamapWidth;
  51.     float zCoord = mapPosition.z * t.terrainData.alphamapHeight;
  52.  
  53.     posX = (int)xCoord;
  54.     posZ = (int)zCoord;
  55.   }
  56.  
  57. void CheckTexture()
  58.   {
  59.     float[,,] aMap = t.terrainData.GetAlphamaps (posX, posZ, 1, 1);
  60.     textureValues[0] = aMap[0,0,0];
  61.     textureValues[1] = aMap[0,0,1];
  62.     textureValues[2] = aMap[0,0,2];
  63.     textureValues[3] = aMap[0,0,3];
  64.   }
  65.   public void PlayFootstep()
  66. {  
  67.   GetTerrainTexture();
  68.  
  69.   if (textureValues[0] > 0)
  70.   {
  71.     source.PlayOneShot(GetClip(stoneClips), textureValues[0]);
  72.   }
  73.   if (textureValues[1] > 0)
  74.   {
  75.     source.PlayOneShot(GetClip(dirtClips), textureValues[1]);
  76.   }
  77.   if (textureValues[2] > 0)
  78.   {
  79.     source.PlayOneShot(GetClip(dirtClips), textureValues[2]);
  80.   }
  81.   if (textureValues[3] > 0)
  82.   {
  83.     source.PlayOneShot(GetClip(dirtClips), textureValues[3]);
  84.   }
  85. }
  86.  
  87. AudioClip GetClip(AudioClip[] clipArray)
  88.   {
  89.     int attempts = 3;
  90.     AudioClip selectedClip =
  91.     clipArray[Random.Range(0, clipArray.Length - 1)];
  92.  
  93.     while (selectedClip == previousClip && attempts > 0)
  94.       {
  95.         selectedClip =
  96.         clipArray[Random.Range(0, clipArray.Length - 1)];
  97.        
  98.         attempts--;
  99.       }
  100.  
  101.     previousClip = selectedClip;
  102.     return selectedClip;
  103.  
  104.   }
  105. }
RAW Paste Data