Hi guys,
I have some code that spawns a bunch of NavMeshAgents (they’re cars with NavMeshAgent components) and sends them to destinations that are specified on the spawnpoints. Each car has the following code in its Update() function:
` ... // Get new navpoint if previous target is reached if (transform.position == target.transform.position || transform.position.z == target.transform.position.z -1 || transform.position.z == target.transform.position.z + 1 || transform.position == (target.transform.position - Vector3(1, 1, 1)) || transform.position == (target.transform.position - Vector3(-1, -1, -1))) { if(target.tag == "Car End Point") { Debug.Log("At endpoint, destory"); Destroy(this.gameObject); NotificationCenter.DefaultCenter().PostNotification(this, "CarDeleted"); } else { //Debug.Log("At navpoint, deciding where to go next..."); if (targetScript.connections.Length > 0) { var element : int = Random.Range(0, targetScript.connections.Length); target = targetScript.connections[element]; //Debug.Log("Connection[" + element + "] chosen."); GetComponent(NavMeshAgent).destination = target.transform.position; } else { GetComponent(NavMeshAgent).destination = target.transform.position + Vector3(30, 0, 0); //Debug.Log("No connections found, proceeding straight as an arrow..."); } } } ... `
I put the || operators because I wanted to make it remove my cars if they come within a 1m of their targets (because they all seem to stop moving when they get to a distance of less than 1m from their targets. The original if statement looked like this:
` if (transform.position == target.transform.position) `
How do I make my NavMeshAgents go all the way to their targets, and/or make them delete themselves if they get within 1m of their targets?
MachCUBED