- Home /
Just cant seem to figure out this simple problem
So I got 6 gameobjects (3 soldier of each team) walking towards each other in a line (3 on the left, walk to the right and 3 on the right that walk to the left) with a rigidbody 2D.
There are two towers on that line, one for each team.
Im trying to make it so the soldiers move and if they collide with a tower, attack it. If they collide with an enemy, attack it. This I can do very easily.
The problem is when multiple soldiers show up.
I want to give attacking soldiers priority in relation to the tower, so if you are attacking the tower you stop and start attacking the enemy soldier.
If there are multiple enemy soldiers the soldier will attack one and when the enemy soldier is dead, attack the other one until no more enemy soldiers are present, at which point the soldier will keep moving foward.
My issue here is the collider thingy to give priority or to remove it, I tried with queues, with OnColliderStay2D, with OnColliderEnter2D but I just cant seem to make it work properly... Anyone can give me a hand?
I tried with coroutines, queues but I always get so tangled up both in code and in my mind that I believe im just complicating the code.
I tried something like this:
int lel = Physics2D.OverlapCircle(transform.position, .8f, new ContactFilter2D(), listResults);
float distanceMax = 0;
if (lel > 0) {
esse.Pause();
foreach (Collider2D esseMesmo in listaDeAtaque) {
float temp = Vector2.Distance(esseMesmo.gameObject.transform.position,transform.position);
if (temp > distanceMax) {
distanceMax = temp;
}
}
}
But it just doesnt work and gets way too complicated, can anyone give me a hand on this?
Answer by DaoGear · Mar 05 at 02:51 PM
Where is your code you filled the list listaDeAtaque. If you are using collision event why do you still using overlapcircle?
ListaDeAtaque is the "listResults", I just forgot to translate it.
Ignoring all the previous code, whats the best way to implement this? I tried so many ways and all of them fail when multiple targets show up as they fail to update properly. Im considering now to use a state machine...
Like a soldier walks in a straight line, if he find a enemy he attacks it. But if there are 3 ally soldiers and 3 enemy soldiers, one might kill the enemy soldier and it wont update in the ally code, thus error. If a new enemy appears I have to make the ally attack that one after attacking the current one, and only when no more enemies are in range will the ally keep moving.
To detect if a tower or an enemy is in attack range, you can use Vector2.Distance() (don't need Physics2D.OverlapCircle, OnColliderStay2D, OnColliderEnter2D). Let's save enemies, towers, allies in 3 arrays, loop through them (in update) then save all the enemies which are in attack ranges. After that, you can apply priority logic to find which one needs to be attacked first.
Your answer
Follow this Question
Related Questions
move camera when it collides with a trigger 1 Answer
OnCollisionEnter Sine Wave to Character 0 Answers
Player not colliding 0 Answers
Help With Colliders 3 Answers
Collision detection without effects and without triggers 1 Answer