- Home /
Raycasting Through Terrain
I need to check to see if an object is about to walk into water. What I need the object to do is turn around so it doesn't walk into it. I've set up a water collider just below the water itself, and I have it so the object is raycasting to the ground slightly in front of him. Unfortunately, since the water collider runs under the terrain, even if the object is on the terrain and the raycast doesn't hit any visible water, it raycasts through the terrain to the water below, and the AI keeps reacting like its about to run into the water. Why is that, and how should I fix it?
it's possible the physics LAYERS system is what you need here.
wont setting the collider above the water work?? or just surronding it... so that when object approaches the water it trrigers??
Anusha .. not really, that would just cause other problems. the only way to use a physics engine is to understand and use the physics "layers" system. there's no other way to get work done!
your english is great BTW !
Even if I used layers, the raycast would go through the terrain and still hit the water.
Answer by PProductions · Sep 04, 2012 at 01:52 PM
Raycasting every frame is not efficient. Simply make the water collider a trigger and use something like the following code to check the collision:
function OnTriggerEnter(object : Collider) {
if(object.tag == "Water") {
//turning code
}
}
Just set up your water collider to have the tag 'Water'. this is the technique used by 'Burg Zerg Arcade' in his video tutorial : Video Tutorial
Thats not the problem. The object has to stop well before it reaches the water which is why it raycasts on the ground in front of it, not below it.
No the problem isn't that its not hitting the water collider. The problem is that it is even when the raycast should just hit the terrain.....which it doesn't. I don't want it to raycast through the terrain. Is that just how Raycasting works? Just ignore the terrain collider? Cause I do have several other scripts where I encounter the problem of it ignoring the terrain hit and passing right through it.
I'm having the same issue. The Raycast ignores the terrain and, as a consequence, my NPCs go right through it, until they reach the terrains lowest height.