static void Main(string[] args) { NN nn = new NN(4, 8, 4); float[] inputs = new float[] { 0.1f, 0.2f, 0.3f, 0.4f }; float[] ar = nn.FeedForward(inputs); }
void FixedUpdate() { float vision = 5f + attackSkill; float[] inputs = new float[inputsCount]; Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, vision); // количество соседних объектов float[] neighboursCount = new float[4]; // вектара к центрам масс еды, красного, зеленого и синего Vector3[] vectors = new Vector3[4]; for (int i = 0; i < 4; i++) { neighboursCount[i] = 0; vectors[i] = new Vector3(0f, 0f, 0f); } for (int i = 0; i < colliders.Length; i++) { if (colliders[i].gameObject == gameObject) { continue; } if (colliders[i].gameObject.name == "food") { neighboursCount[0]++; vectors[0] += colliders[i].gameObject.transform.position - transform.position; } else if (colliders[i].gameObject.name == "bacterium") { AI ai = colliders[i].gameObject.GetComponent <AI>(); neighboursCount[1] += ai.attackSkill / 3f; vectors[1] += (colliders[i].gameObject.transform.position - transform.position) * ai.attackSkill; neighboursCount[2] += ai.foodSkill / 3f; vectors[2] += (colliders[i].gameObject.transform.position - transform.position) * ai.foodSkill; neighboursCount[3] += ai.defSkill / 3f; vectors[3] += (colliders[i].gameObject.transform.position - transform.position) * ai.defSkill; } } for (int i = 0; i < 4; i++) { if (neighboursCount[i] > 0) { vectors[i] /= neighboursCount[i] * vision; inputs[i] = vectors[i].magnitude; } else { inputs[i] = 0f; } } float[] outputs = nn.FeedForward(inputs); Vector2 target = new Vector2(0, 0); for (int i = 0; i < 4; i++) { if (neighboursCount[i] > 0) { Vector2 dir = new Vector2(vectors[i].x, vectors[i].y); dir.Normalize(); target += dir * outputs[i]; } } if (target.magnitude > 1f) { target.Normalize(); } Vector2 velocity = rb.velocity; velocity += target * (0.25f + attackSkill * 0.05f); velocity *= 0.98f; rb.velocity = velocity; float antibiotics = 1f; // концентрация антибиотиков // if(transform.position.x < -39) antibiotics = 4; // else if(transform.position.x < -20) antibiotics = 3; // else if(transform.position.x < -1) antibiotics = 2; // antibiotics = Mathf.Max(1f, antibiotics - defSkill); energy -= Time.deltaTime * antibiotics * antibiotics; if (energy < 0f) { Kill(); } }