Home > Net >  Check if can move to the target in navmesh
Check if can move to the target in navmesh

Time:11-13

So, I have a ball and some obstacles, which disallow the ball to move to the target. Ball is shotting bullets and after each shot I'm checking if the ball can reach the target. I need the ball to stay on starting position until the path is clear. Unfortunately, even if some obsacles are on the way, the ball keeps moving until countering them.

That's what I've tried to do: `

    private void TryReachTheTarget()
    {
        if (NavMesh.CalculatePath(_agent.transform.position, _target.position, NavMesh.AllArea s,           _path) && _path.status == NavMeshPathStatus.PathComplete)
        {
            _agent.SetPath(_path);
        }
    }

`

enter image description here

CodePudding user response:

You can just try to find a path and return if it was or was not successful. Checking for a path is a little slow, but as long as you aren't shooting hundreds of times per second this should be fine.

// your agent
public NavMeshAgent agent; 
// variable for storing the path
private NavMeshPath navMeshPath = new NavMeshPath();

// tries to find a path, and returns true if successful
public bool CanReachArea(Vector3 target) {
    return agent.CalculatePath(target, navMeshPath) && navMeshPath.status == NavMeshPathStatus.PathComplete;
}

CodePudding user response:

The solution appeared to be really simple. All I needed was taking off Obstacle component from the target.

  • Related