Making an AI Flee

private void Flee(List objectsToFlee)
{
Vector3 fleeVector = Vector3.zero;

        foreach (GameObject obj in objectsToFlee)
        
        fleeVector += (obj.transform.position + other obj that are in range.transform.position) / number of obj in range
}

Movetwards(- fleeVector);

}
anyone know how to say this? like how could i say all obj in range with more mass than this gameobject added together then divided by the number of those gameobjects. Im making an Ai and I want it to run away from any gameobject with more mass than itself. its not that hard to make it run away if one enemy comes in range, but if 2 or more enemies come in range, i need it to find the best way to run away

You can use Physics.OverlapSphere with your object position and a range, and compute your condition by using the array given by this method to know if you need to move away

Hope it helps ! Good Luck for your AI

private void Flee(List objectsToFlee)
{
Vector3 fleeVector = Vector3.zero;

        foreach (GameObject obj in objectsToFlee)
        {
        fleeVector += (obj.transform.position - this.transform.position);
        }
    fleeVector /= objectsToFlee.Count;
    Debug.Log("Run");

    MoveTowards(-fleeVector);
    }

    private void MoveTowards(Vector3 target)
    {
        transform.position = Vector3.MoveTowards(transform.position, target, moveSpeed * Time.deltaTime);
    }

i tried this, it kind works but not that great