I have a list of targets that can be picked depending on the magnitude from the vector created using:
for(int i = 0; i < targetQueue.Count; i++) //2 loops to cycle through targets
{
for(int j = 0; j < targetQueue.Count; j++)
{ //if two targets are the same, then skip them
if(targetQueue[i] != targetQueue [j])
{
//getting the direction using the source and destination vectors
Vector3 iDir = targetQueue[i].transform.position - transform.position;
Vector3 jDir = targetQueue[j].transform.position - transform.position;
//comparing i with j, if i is less than target at j then match found
if(iDir.magnitude < jDir.magnitude)
{ //LockedTarget is a gameobject to show what target the turret is locked on to
LockedTarget = targetQueue[i]; //if match found then set locked target to target at i
break;
}
}
}
}
This code compares every target to each other, however it does this twice, once on each side( target[i]
becomes target[j]
). When the magnitude is compared, if the target[i]
is smaller, then this is now the locked target as this target is closer to the turret.
This works every time except for one instance and I can't figure out why its happening here:
When both targets are inside the collider, the target on the right should still be highlighted, yet I get the target that is farther away being selected, this also occurs with more than two targets, with even stranger results. These being that the original target chosen for 'DEADLOCK' what i'm calling it, stays locked. Even when I move the other targets above, below, left and to the right of it.
This only occurs with this target, i've tried with 2 other targets (south of the turret) and it works as intended by properly switching based on size of magnitude of the direction vector between the target and the source.
I have ruled out the possibility being that it is an issue to do with adding target's to the gameobject list as it works everywhere else. When measuring the lengths of the magnitudes, the wrong target is chosen as the locked target. It does show that the closest target on the images does actually have the smallest vector magnitude.
UPDATE: the magnitudes of the vectors are increasing and decreasing by tiny amounts, 1 to 2 for a major movement. this makes sense as its worldPos. What doesn't make sense is the target on the right having a magnitude of 49 and the target above having a magnitude of 82, when according to world space they are pretty close to eachother.
Can anyone help me with this issue? its been driving me nuts for days.
It must be to do with the magnitudes of the direction vectors not being calculated properly.