Hello, I am trying to create a simple movement script for bots. I created NavMesh, baked all static objects and added the agent.
Automatically I added some points which are the desirable locations for the bot to reach. Some of them lie outside the NavMesh. Is it possible to check: whether a target point is reachable or not? (lies in the NavMesh or outside)
Thank you very much!
Gatau
July 18, 2015, 12:57pm
2
The NavMesh.CalculatePath
should be able to find which nodes are reachable inside the area mask.
http://docs.unity3d.com/ScriptReference/NavMesh.CalculatePath.html
Thank you for your help! The problem was in coordiates. I had different coordinates (local) for cubes and global for agent.
Here’s the working script to go from one point to another.
using UnityEngine;
using System.Collections;
public class randomcubepathgeneration : MonoBehaviour {
//all target points
public Transform target1;
public Transform target2;
public Transform target3;
public Transform target4;
public Transform target5;
public Transform target6;
public Transform target7;
public Transform target8;
public Transform target9;
public Transform target10;
public Transform target11;
public Transform target12;
public Transform target13;
public Transform target14;
public Transform target15;
public Transform target16;
public Transform target17;
public Transform target18;
public Transform target19;
public Transform target20;
public Transform target21;
public Transform target22;
public Transform target23;
public Transform target24;
public Transform target25;
public Transform target26;
public Transform target27;
public Transform target28;
public Transform target29;
public Transform target30;
//create array from points
public Transform[] allpoints;
public int randomnumber;
public NavMeshAgent agent;
public NavMeshPath path;
public NavMeshPathStatus status;
public bool reachable = false;
public bool hasFoundPath;
public bool anewpath;
public bool doonce = true;
// Use this for initialization
void Start () {
allpoints = new Transform[30];
allpoints [0] = target1;
allpoints [1] = target2;
allpoints [2] = target3;
allpoints [3] = target4;
allpoints [4] = target5;
allpoints [5] = target6;
allpoints [6] = target7;
allpoints [7] = target8;
allpoints [8] = target9;
allpoints [9] = target10;
allpoints [10] = target11;
allpoints [11] = target12;
allpoints [12] = target13;
allpoints [13] = target14;
allpoints [14] = target15;
allpoints [15] = target16;
allpoints [16] = target17;
allpoints [17] = target18;
allpoints [18] = target19;
allpoints [19] = target20;
allpoints [20] = target21;
allpoints [21] = target22;
allpoints [22] = target23;
allpoints [23] = target24;
allpoints [24] = target25;
allpoints [25] = target26;
allpoints [26] = target27;
allpoints [27] = target28;
allpoints [28] = target29;
allpoints [29] = target30;
agent = GetComponent<NavMeshAgent> ();
path = new NavMeshPath(); //without this declaration NavMesh.CalculatePath doesn't work (it doesn't even show FALSE)
anewpath = true;
if (anewpath = true) {
Debug.Log ("new target");
//choosing random point
randomnumber = Random.Range (0, allpoints.Length);
hasFoundPath = agent.CalculatePath (allpoints [randomnumber].position, path);
Debug.Log (hasFoundPath);
anewpath = false; //wait till the player gets to the point
}
}
// Update is called once per frame
void Update () {
if((path.status == NavMeshPathStatus.PathComplete) && (doonce == true));
{
print("The agent can reach the destionation");
reachable = true;
Debug.Log (status);
doonce = false;
if (reachable) {
//go to this point (the action continues even after MB was pressed)
agent.SetDestination (allpoints[randomnumber].transform.position);
}
}
if((transform.position.x == allpoints[randomnumber].transform.position.x)&&(transform.position.z == allpoints[randomnumber].transform.position.z)){
Debug.Log ("reached destination");
anewpath = true;
doonce = true;
if(anewpath){
randomnumber = Random.Range (0, allpoints.Length);
hasFoundPath = agent.CalculatePath (allpoints [randomnumber].position, path);
Debug.Log (hasFoundPath);
anewpath = false;
}
}
}
}