- Home /
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.
Answer by Loius · Feb 17, 2013 at 11:27 PM
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.
Yeah it only needs to scan the once, and SampleHeight seems to be alot faster than raycasting, so i suppose doing a scan in a spiral pattern wouldnt be too inefficient.
I also have no experience of Unitys pathfinding, but i am using arongranbergs A* and it seems its possible to "Return the nearest node to a position using the specified NNConstraint", the specified NNConstraint being editable like this, which seems like it could be an answer.
So i guess its not possible to block a spherecast with a collider? Seems a shame, functionality like that would make things like line of sight checks a doddle.
Thanks for your suggestions, got me thinking.