- Home /
Detect Different Terrain Texture?
Hello,
I have a simple tower defence game, with a path leading from one end of the scene, to another end - the enemy follow this path using waypoints. The user can place a turret anywhere around the scene at the moment. But is it possible not to put a turret on the path? My path uses a different terrain texture (rock) where as my 'placeable' terrain is grass - how would I tell unity not to allow the turret to be placed on anything with the texture 'rock'?
To place a turret, I use a simple raycast to determine where the user is clicking and then initiate a prefab on that position (snippet) :
function Update () { var ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hit : RaycastHit; var layerMask = 1 << 8; if (chooseLocation) { layerMask = ~layerMask; Physics.Raycast (ray, hit, 500, layerMask); currentObj.Translate((Vector3(hit.point.x, currentObj.position.y, hit.point.z) - currentObj.position) * Time.deltaTime *followSpeed); } if (Input.GetButtonDown("Fire1")){ //Input.GetMouseButton(0) chooseLocation = false; // maybe a : if(terrain texture = grass? }
// Cancel Selection \\
if(Input.GetButtonDown("Fire2") && currentObj.gameObject){
objLimit[currentSelected] ++;
chooseLocation = false;
Destroy(currentObj.gameObject);
}
}
Maybe I dont have to use terrain texture? Is there another way to do it?
Thanks folks!
Answer by Soviut · Apr 29, 2011 at 03:01 AM
Using the texture isn't a very good way to do it since any variation in the texture could throw the calculation off.
Instead, simply create box colliders between the waypoints that are as wide as the path. Then, when the mouse is released, check to see if the tower you're dragging is colliding with any of the boxes.
You should put the registration point on the "back" face of the box collider. This way, when you scale it, it only scales in one direction.
Next, position the box at waypoint A. Aim it at waypoint B using your favourite look at formula.
Lastly, subtract the position of A from B to get a relative vector. With this, use it's length to determine how far out to scale each box to have it touch the next waypoint.
Answer by DaveA · Mar 31, 2011 at 03:46 PM
The ray hit should give you some texture info. I've not worked with terrains that much yet, but if the color is not enough, I wonder if it's possible to have a hidden layer, or alpha channel on one that isn't, that indicates go/no-go for that.
Another way is to put invisible objects (colliders) along the ground where you don't want things and have them signal the no-go when clicked.