Detect closest reachable point of object

Evening all,

Im trying to give some level of awareness to the npc in my scene, in this case detecting where the nearest water is from its current position on the terrain. The water is a single plane stretched across the whole scene at a height of 0.

Now i believe a Physics.SphereCastAll down from above the npcs position would be a real nice solution if there were only a way of blocking the cast going through the terrain, as currently i can only ever retrieve the hit.point of the Water directly below the npc, which is obviously not cool or reachable.

  • So i suppose the question is, is there any way to block a SphereCast from passing through a collider?

  • Failing that, is there any other (or better) method to achieving my goal?

The only other method im familiar with is iterating through a stream of Terrain.SampleHeight or Physics.Raycast until Y=0, but that seems very costly for what should be just a quick scan

Any advice welcome.

Yikes, that’s a tricky one. Two options spring to mind -

Use Unity’s pathfinding (I have zero experience with this) and somehow keep track of what positions are wet enough, then seek those out.

If you height-sample or raycast (down at an angle, in a circle), you can test in a sort of polka-dot pattern. Since you’re looking for a body of water of some size, you can space out your tests by half of that size as you expand your search from “you”, and be reasonably certain that you’ll hit the closest thing. It should be easy to make the spacing large enough to make the operation pretty quick, provided it doesn’t have to get called every frame.