- Home /
How to calculate the very next position on a NavMesh to a cast ray outside the NavMesh?
The situation is: I have a world with a NavMesh attached to it. If I click somewhere in the world the NavMeshAgent moves the character as near as possible to the clicked position. Fine. But the problem is that the Physics.Raycast()-function returns false if I click somewhere outside my "world". Ok that's fine, too. But my question is now how to get the very next position on my NavMesh to the cast ray.
// Collect all mouse click data.
MouseClickData data = new MouseClickData();
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
data.WorldPosition = hit.point;
data.ClickedObject = hit.collider.gameObject;
}
data.ScreenPosition = Input.mousePosition;
translate mouse coord into world space, drop its Y value to the level of the terrain and iterate through your meshes verts to find the nearest (or perhaps, grow a collider out from the point and wait till you hit an object).
Do you want the closest valid position to where you're clicking?
Yes, of course. I want the closest valid position on my plane or what ever lies next to my click in the "empty world".
To check step by step whether there is an valid point at the left, right, top or bottom is not a solution because there is always a blind angle.
To circle around the point is also a bad solution because in the case of clicking very far away the circle grows a lot and the position where it will find a valid point will be too far away from the position where we would expect the character to go.
$$anonymous$$y next idea would be the to cast a ray from the invalid position (on screen) to the character and then from each of its points a ray down in the scene until a valid position is found. But again there is a problem because we have no guarantee that the character is always in the scene.
I've tried this: http://answers.unity3d.com/questions/294953/search-for-nearest-point-of-object-to-mouseclick.html (last comment) But it seemed to work not properly or not at all. $$anonymous$$ost of the time the character just walks to the center of the plane and not to the borders as I would expect it after clicking in the area behind a border in the empty space. Or maybe I did something wrong. :-/
When you're clicking outside the world, is there any collider at all? That would be the easiest way to get a position... have an invisible collider.
Answer by urg0tt · Aug 23, 2012 at 08:27 AM
I've tried putting a invisible plane under "my world". This approach works more or less well for clicks next to "my world", but clicks far away seem to be thrown away. At least the character (or the NavMeshAgent) doesn't move. An other disadvantage is that the camera always needs to be positioned carefully because otherwise there would be a chance having "empty space" again.
I've also tried to put "my world", including the camera, completely inside a sphere collider, but this doesn't work at all because sending a ray will result in getting an answer immediately as we are already inside a collider.
My current solution is casting a ray from mouse position on screen to the scene. If the ray hits something, everything is fine and the character moves as near as possible. Otherwise I check for every point of the ray the nearby colliders, find the closest collider and save its distance to the point. After all I know the distances of all points to their closest collider. I take the point with the shortest distance, get its closest collider and move the character to a collider's point which lies next to the point of the ray. This approach results in moving my character relatively precisely to a position I would expect. But the algorithm might have performance issues.
So I solved my problem myself. Anyway, thank you for trying to help me. But if you have some further ideas for optimization, just let me know it.
Your answer
Follow this Question
Related Questions
Hit distance Change light range 1 Answer
How to hit two object with one raycast? 2 Answers
Need help with Third person shooter 0 Answers
Ray from enemy to player is always a bit off (Image) 1 Answer
RaycastHit.distance 1 Answer