1
\$\begingroup\$

I have a mock environment set up with a cube that has a specified "Not Walkable" layer. I'm using NavMeshAgent and NavMeshPath to set destinations of the player. The way I'm setting paths is by ray-casting points on the terrain by mouse clicking.

I'm having trouble figuring out a way to fix if I click on the cube that has a "Not Walkable" layer, the player just runs into the cube and tries to reach that point.

I've read about the FindClosestEdge function in unity but I don't think that's exactly what I'm looking for.

So basically I need the target location to change to the nearest point next to the cube when I click on the cube.

Any ideas?

\$\endgroup\$
  • \$\begingroup\$ One possible fix - is to have the nagivation mess as one solid piece within all predictably clickable areas. That is to say, if there is an obstruction where the player has clicked in an area within the level boundaries, then you just leave the navmesh, but you have a special tag. When the player then clicks the obstructed area, he is still clicking on the navmesh and generating a path to that point. The only difference is the path finding program will clip the path back a few paces to compensate. A quick and dirty method, but still a possible solution. \$\endgroup\$ – moonshineTheleocat Nov 19 '15 at 16:14
1
\$\begingroup\$

I'm wondering about this too! Sad to see there aren't any replies.

I did something similar -- parts not included for walking in the navmesh, like static objects in the scene, would be a problem when my agent would get to them and just continue to walk and walk.. and walk. Maybe this can be of use to you.

For now, I worked something up so that my agent walks as close as it can to an area that's not on the navmesh then stops and I clear the path.

I put these in an if statement in my update and check to see if the destination has been reached. Checking if the type of path is partial (I think: part of it is unreachable) and then if we have a path to the desired position (it seems to be true if we can do any more movement, then false when we reach the block).

agent.pathStatus == NavMeshPathStatus.PathPartial && !agent.hasPath

This way, even when I click on something that's not walkable, like inside a block, it walks as close as it can and stops. Then I have it abandon the path.

RAPTOR BLOCK!

Here's what I'm using, until I can find something better. It checks if the agent has reached the destination. There's also my code in that block to see if the agent has reached the destination when the path isn't partial so that I can control my walking and idle animations. isMoving is set when a destination is set for the agent. It's kind of a mouthful. The stuff relevant to the partial path is above, you can check it out in use below if you're interested.

   if (isMoving && !agent.pathPending)
    {
        if ((agent.pathStatus == NavMeshPathStatus.PathPartial && !agent.hasPath) ||
            ((Vector3.Distance(agent.destination, agent.transform.position) <= agent.stoppingDistance) &&
             (!agent.hasPath || agent.velocity.sqrMagnitude == 0f)))
        {
            Debug.Log("reached destination");
            isMoving = false;
            anim.SetBool("IsRunning", isMoving);

            agent.ResetPath(); 
        }

    }

I'm still new to unity, so I'm not sure if this is the optimal way that this could be done. If anyone has input, I'd be happy to hear it!

EDIT: I was messing around a bit (something smelled off...) and I think some of that code was redundant. I tested it under the same conditions as the other code and I get the same results with just:

   if (isMoving && !agent.pathPending && !agent.hasPath)
    {
        Debug.Log("reached destination");
        isMoving = false;

        anim.SetBool("IsRunning", isMoving);
        agent.ResetPath(); 
    }
\$\endgroup\$
0
\$\begingroup\$

I'm working on a similar problem and although I haven't tested it yet, I think you can use NavMesh.Raycast starting from the user's position and place the cube wherever the ray hit's position is. Hope this helps.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.