Files
HauntedBloodlines/Assets/Scripts/Sound Scripts/CheckTerrainTexture.cs
2025-05-29 22:31:40 +03:00

65 lines
1.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CheckTerrainTexture : MonoBehaviour
{
//This scripts goes inside the people/Animals or whatever to detect the terrain's textures.
//(Do not use it with singlenton! There's no point).
public Transform playerTransform;
public Terrain terrain;
public int posX;
public int posZ;
public float[] textureValues;
void Start()
{
terrain = Terrain.activeTerrain;
playerTransform = gameObject.transform;
}
void Update()
{
// For better performance, move this out of update
// and only call it when you need a footstep.
//GetTerrainTexture();
}
public void GetTerrainTexture()
{
ConvertPosition(playerTransform.position);
CheckTexture();
}
void ConvertPosition(Vector3 playerPosition)
{
Vector3 terrainPosition = playerPosition - terrain.transform.position;
Vector3 mapPosition = new Vector3
(terrainPosition.x / terrain.terrainData.size.x, 0,
terrainPosition.z / terrain.terrainData.size.z);
float xCoord = mapPosition.x * terrain.terrainData.alphamapWidth;
float zCoord = mapPosition.z * terrain.terrainData.alphamapHeight;
posX = (int)xCoord;
posZ = (int)zCoord;
}
void CheckTexture() //Here's all the textures of the terrain.
{
float[,,] aMap = terrain.terrainData.GetAlphamaps(posX, posZ, 1, 1);
textureValues[0] = aMap[0, 0, 0];
textureValues[1] = aMap[0, 0, 1];
textureValues[2] = aMap[0, 0, 2];
textureValues[3] = aMap[0, 0, 3];
textureValues[4] = aMap[0, 0, 4];
textureValues[5] = aMap[0, 0, 5];
textureValues[6] = aMap[0, 0, 6];
textureValues[7] = aMap[0, 0, 7];
textureValues[8] = aMap[0, 0, 8];
textureValues[9] = aMap[0, 0, 9];
}
}