# Detecting what parts of the terrain was hit.

Hi.

In my game I have a terrain. I also have guns that fires using raycast and I would like to Instantiate different particles at hit.point of the raycast. But since I am dealing with a terrain how do I distinguish wheater or not I am shooting grass, sand or rock painted on the terrain?

If you get what I mean.

Sorry this is a little hastly written bacause I have to hurry out the door. If this has not been answered when I come back I will re-formulate my self.

This comes from tested code, but I may have tweaked it and broken it. In my particular case, I’m checking a transform named puffPose, and grass is texture #5, which is [0,0,4]:

``````// Set up:
Vector3 TS; // terrain size
Vector2 AS; // control texture size

TS = Terrain.activeTerrain.terrainData.size;
AS.x = Terrain.activeTerrain.terrainData.alphamapWidth;
AS.y = Terrain.activeTerrain.terrainData.alphamapHeight;

// Lookup texture we are standing on:
int AX = (int)((puffPos.x/TS.x)*AS.x+0.5f);
int AY = (int)((puffPos.z/TS.z)*AS.y+0.5f);
float[,,] TerrCntrl = Terrain.activeTerrain.terrainData.GetAlphamaps(AX, AY,1 ,1);
// This can get a grid. Since we are only getting 1, we have a 1x1 array
// The 3rd is the 0-1 weight of that texture (if you have 4 textures, the 3rd
//   has size 4. TerrCntrl[0,0,0] is the weigth of texture#0.)
// TC[0,0, 0-??] add to 1
float c1=TerrCntrl[0,0,4];
if(c1>0.5f) ....  // grass is 50% or more in this area
``````

I think you would just be after the Splat Texture and then go from there. What I’m unsure about is how you distinguish which texture is applied to a particular spot on the terrain; the property returns an array, but I’m not familiar enough with TerrainData to know how to use that properly… Maybe someone else can offer some insight on that.