public float CheckEnemies() { dist = Mathf.Infinity; target = null; float rating = 0; var scripts = FindObjectsOfType <MonoBehaviour>().OfType <WWSeeker>(); foreach (WWSeeker s in scripts) { if (s.team != team && s.alive) { var dist1 = (transform.position - s.transform.position).magnitude; //formula 0.1/x+0.1 (0-1) rating += Mathf.Clamp(0.1f / dist1 + 0.1f, 0, 1); //get closest enemy if (dist1 < dist) { dist = dist1; closest_enemy = s; } } } return(Mathf.Clamp(rating, 0, 1)); }
public float CheckAllies() { target = null; float rating = 0; var scripts = FindObjectsOfType <MonoBehaviour>().OfType <WWSeeker>(); foreach (WWSeeker s in scripts) { if (s.team == team && s.alive) { var dist1 = (transform.position - s.transform.position).magnitude; //formula 0.1/x+0.1 (0-1) rating += Mathf.Clamp(0.1f / dist1 + 0.1f, 0, 1); } } return(Mathf.Clamp(rating, 0, 1)); }
private void ExecuteAction(int action) { int total = 0; Vector3 pos = new Vector3(0, 0, 0); target = null; switch (action) { case 0: //idle state = "idle"; break; case 1: //attack Debug.Log("Attack"); target = closest_enemy; state = "attack"; if (closest_enemy != null) { movementController.destination = target.transform.position; movementController.canSearch = true; } break; case 2: // flee Debug.Log("Flee"); state = "flee"; scripts = FindObjectsOfType <MonoBehaviour>().OfType <WWSeeker>(); foreach (WWSeeker s in scripts) { if (s.team != team && s.alive) { total++; pos += s.transform.position; } } movementController.destination = -pos / total; movementController.canSearch = true; break; case 3: // regroup // move to the closest team member Debug.Log("Regroup"); state = "regroup"; scripts = FindObjectsOfType <MonoBehaviour>().OfType <WWSeeker>(); var dist = Mathf.Infinity; var closest_ally = this; foreach (WWSeeker s in scripts) { if (s.team == team && s.alive) { var dist1 = (transform.position - s.transform.position).magnitude; //get closest enemy if (dist1 < dist) { dist = dist1; closest_ally = s; } } } if (closest_ally != this) { movementController.destination = closest_ally.transform.position; movementController.canSearch = true; } break; } }