- Home /
make an AI Flee
so im working on a game like agar.io, im making an AI that has 3 states (FindFood, Chase, Run) i have little pieces of food that spawn all over the map, and the AI will chase the closest pice of food. Then if a player comes in range, and has less mass than this AI, the AI will chase that player until it either eats it, or it gets out of range. Now i need to make "Run" which will be if a player with more mass comes into range, this player will run away. But the problem, is i need it to be able to run from multiple players at once, cus say that 2 players come in range with more mass, it can't just run in the opposite direction of bot, i would need it to run in-between them, but i don't know how to do this. here is my script
public float timer;
private float dist;
private GameObject currentTarget;
private GameObject enemy;
private GameObject currentFood;
public float Speed = 20f;
public GameObject[] Targets;
public GameObject[] Foodtarget;
public Rigidbody rb;
public Rigidbody otherRB;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
Targets = GameObject.FindGameObjectsWithTag("Player");
Foodtarget = GameObject.FindGameObjectsWithTag("Food");
timer += Time.deltaTime;
{
currentTarget = ChooseNewEnemy();
currentFood = ChooseFood();
StopAllCoroutines();
if (currentTarget != null && rb.mass > otherRB.mass)
{
if (rb.mass > otherRB.mass)
{
StartCoroutine(ChaseTarget(currentTarget));
}
Debug.Log("Player");
}
else if (currentFood != null)
{
StartCoroutine(ChaseFood(currentFood));
Debug.Log("Food");
}
}
}
GameObject ChooseNewEnemy()
{
GameObject possibleTarget = null;
float closestDistanceSqr = Mathf.Infinity;
Vector3 currentPosition = transform.position;
foreach (GameObject other in Targets)
{
Vector3 directionToTarget = other.transform.position - currentPosition;
dist = Vector3.Distance(transform.position, other.transform.position);
float dSqrToTarget = directionToTarget.sqrMagnitude;
if (dist < 15f && dist > 0.5f && dSqrToTarget < closestDistanceSqr)
{
otherRB = other.GetComponent<Rigidbody>();
closestDistanceSqr = dSqrToTarget;
possibleTarget = other;
}
}
return possibleTarget;
}
IEnumerator ChaseTarget(GameObject target)
{
if (target == null)
{
yield break;
}
while (target == currentTarget && rb.mass > otherRB.mass)
{
transform.position = Vector3.MoveTowards(transform.position, target.transform.position, Speed * Time.deltaTime);
yield return null;
}
while (target == enemy && rb.mass > otherRB.mass)
{
transform.position = Vector3.MoveTowards(transform.position, target.transform.position, Speed * Time.deltaTime);
yield return null;
}
}
GameObject ChooseFood()
{
GameObject possibleFood = null;
float closestDistanceSqr = Mathf.Infinity;
Vector3 currentPosition = transform.position;
foreach (GameObject other in Foodtarget)
{
Vector3 directionToTarget = other.transform.position - currentPosition;
dist = Vector3.Distance(transform.position, other.transform.position);
float dSqrToTarget = directionToTarget.sqrMagnitude;
if (dSqrToTarget < closestDistanceSqr)
{
closestDistanceSqr = dSqrToTarget;
possibleFood = other;
}
}
return possibleFood;
}
IEnumerator ChaseFood(GameObject food)
{
if (food == null)
{
yield break;
}
while (food == currentFood)
{
transform.position = Vector3.MoveTowards(transform.position, food.transform.position, Speed * Time.deltaTime);
yield return null;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612172517im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
AI fleeing from multiple enemies 0 Answers
Resize Array Based on Value 2 Answers
Help with AI script 0 Answers
Properties of Artificail Intellegence in Unity Game Engine 0 Answers