- Home /
Error on script for detecting the terrain texture
Hey everyone. I'm new to Unity an I'm having problems with this script for detecting the terrain texture under the player. I''m using the script that I've found here: https://gamedevbeginner.com/terrain-footsteps-in-unity-how-to-detect-different-textures/ When I run the game I get error: T exture rectangle is out of bounds (824 + 1 > 512) UnityEngine.TerrainData:GetAlphamaps(Int32, Int32, Int32, Int32)
(From what I can tell the 824 is the posZ and the 512 is one of the map dimensions.) the error is on this line: float[,,] aMap = t.terrainData.GetAlphamaps(posX, posZ, 1, 1); the script looks like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CheckTerrainTexture : MonoBehaviour
{
public Transform playerTransform;
public Terrain t;
public float mapx = 0;
public float mapy = 0;
public int posX;
public int posZ;
public float[] textureValues;
void Start()
{
t = 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();
mapx= t.terrainData.alphamapWidth;
mapy= t.terrainData.alphamapHeight;
}
public void GetTerrainTexture()
{
ConvertPosition();
CheckTexture();
}
void ConvertPosition()
{
Vector3 terrainPosition = playerTransform.position - t.transform.position;
Vector3 mapPosition = new Vector3
(terrainPosition.x / t.terrainData.size.x, 0,
terrainPosition.z / t.terrainData.size.z);
float xCoord = mapPosition.x * t.terrainData.alphamapWidth;
float zCoord = mapPosition.z * t.terrainData.alphamapHeight;
posX = (int)xCoord;
posZ = (int)zCoord;
}
void CheckTexture()
{
float[,,] aMap = t.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];
}
}
Can someone please help me fix this problem?
Answer by Pitic · May 12, 2020 at 03:17 PM
Heres my code, i fixed it, the problem you have is that you are not on the terrain, therefore it cant see any terrain texture below you, heres my code i have fixed the issues and added sounds :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CheckTerrainTexture : MonoBehaviour {
public Transform playerTransform;
public Terrain t;
public int posX;
public int posZ;
public float[] textureValues;
public AudioSource source;
public AudioClip[] stoneClips;
public AudioClip[] dirtClips;
public AudioClip[] sandClips;
public AudioClip[] grassClips;
AudioClip previousClip;
void Start ()
{
t = 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 - t.transform.position;
Vector3 mapPosition = new Vector3
(terrainPosition.x / t.terrainData.size.x, 0,
terrainPosition.z / t.terrainData.size.z);
float xCoord = mapPosition.x * t.terrainData.alphamapWidth;
float zCoord = mapPosition.z * t.terrainData.alphamapHeight;
posX = (int)xCoord;
posZ = (int)zCoord;
}
void CheckTexture()
{
float[,,] aMap = t.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];
}
public void PlayFootstep()
{
GetTerrainTexture();
if (textureValues[0] > 0)
{
source.PlayOneShot(GetClip(stoneClips), textureValues[0]);
}
if (textureValues[1] > 0)
{
source.PlayOneShot(GetClip(dirtClips), textureValues[1]);
}
if (textureValues[2] > 0)
{
source.PlayOneShot(GetClip(dirtClips), textureValues[2]);
}
if (textureValues[3] > 0)
{
source.PlayOneShot(GetClip(dirtClips), textureValues[3]);
}
}
AudioClip GetClip(AudioClip[] clipArray)
{
int attempts = 3;
AudioClip selectedClip =
clipArray[Random.Range(0, clipArray.Length - 1)];
while (selectedClip == previousClip && attempts > 0)
{
selectedClip =
clipArray[Random.Range(0, clipArray.Length - 1)];
attempts--;
}
previousClip = selectedClip;
return selectedClip;
}
}
Your answer
Follow this Question
Related Questions
Random: NullReferenceException: Object reference not set to an instance of an object 2 Answers
"CheckDisalowAllocation" Error using ShaderGraph 0 Answers
Can't access rigidbody commands 'Rigidbody' does not contain a definition for... 2 Answers
Cannot paint textures onto terrain, errors are raised 0 Answers
Can scripts be attached to speedtrees? 0 Answers