- Home /
I need to write a code where it takes 6 distance inputs and orders them based on their proximity.
Basically, I need to write a code where lets say, there is 3 enemies, and in the unity console bar, it should appear ordering them based on their proximity.
For example: Enemy1 is closer to you.
Answer by roojerry · Mar 18, 2013 at 03:49 PM
Take the Vector.Distance() between each enemy and the player, then order them in a list or array from smallest distance to largest.
How can i do it if this is just the beginning of the script and i just want it to appear on the console? and also not ordering them myself but the console orders them based on their proximity?
o, well then what deter$$anonymous$$es which enemy is closer and why does it need to be printed to the console?
I know that's not quite an answer, but I'd advise you to look for a tutorial on some of the standard sorting algorithms, for instance "Bubble Sort" or "Quick Sort", since you will need them regularly. I don't have a good one at hand right now, but there are many for those out there.
How can you see if any enemy is closer that the other if they don't exist? This is impossible to understand.
not ordering them myself but the console orders them based on their proximity?
also i should have stated, there are no objects in unity, its just coding so there are really no enemies or player, its just the script
Total contradiction. This makes no sense.
If you have 3 gameObjects, then they have transform components. So you find the distance between the transform position of each one in relation to the player object. So this will give 3 distances. Then you have to sort these distances in code to organize them in order based on distance. This has been explained by brianruggieri, PsychoHead, and now myself. (upvote to you both!)
If you cannot describe what you are trying to do, then there is absolutely no way that we can guess. If the problem is English is not your native language, try and write a better description in English, then under it post the same question in your own language, there may be someone here who can understand and explain to the rest of us exactly what you are trying to do.
Here are some links I strongly suggest to all new users :
Start at the bottom and work up : http://www.unity3dstudent.com/category/modules/
this is the YouTube link for the above as one playlist : http://www.youtube.com/watch?v=-oXYHNSmTxg&list=PL27B696FB515608D2&feature=plcp
That is good to get started. Then start with a small tutorial, this is a simple 2D space shooter : http://www.unityjumpstart.com/ProofOfConcept_1/ : click on the videos part1.mp4 part2,3,4 =]
I found another by Eric : http://wiki.unity3d.com/index.php?title=2DShooter : http://forum.unity3d.com/threads/7883-2D-shooter-tutorial
By then you should be getting the hang of things and starting to have ideas of your own. When you decide what kind of game you want to make, then look at each part you'll need. For example, if you want to make some terrain then walk around it with a character : http://cgcookie.com/unity/2011/12/05/introduction-to-character-controllers/
Basically then just search for tutorials, there are many out there, either written or on youtube.
the Unity Wiki tutorials : http://wiki.unity3d.com/index.php/Tutorials
A big list of tutorials : http://answers.unity3d.com/questions/12321/how-can-i-start-learning-unity-fast-list-of-tutori.html
A very helpful 'site, all in C# : http://unitygems.com/
Helpful page with information on using Built-In Arrays and Lists (you'll need this later!) : http://www.unifycommunity.com/wiki/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?
The unity wiki link above is very handy with lots of scripts and shaders too (just check out all the links down the left, and the tabs along the top : http://wiki.unity3d.com/index.php/Scripts )
http://answers.unity3d.com/questions/148211/list-of-frequently-asked-beginners-questions.html
http://forum.unity3d.com/threads/132628-How-to-help-the-Absolute-Beginner
Answer by Owen-Reynolds · Mar 18, 2013 at 05:27 PM
With only two enemies, you can use one if with prints. With 3+, you have to take it seriously. For serious, you separate things out. Step one is to get them sorted. Then you can easily do whatever else (print to console, find closest, ... )
Standard sorting says to first put all the items in an array. For the array, you don't just want to know "the closest is 4.6 away." You probably want to know "Orc Steve is closest, which is 4.6." So, it makes sense to put the whole enemy in the array, not just the location. A whole enemy is the Transform (or the gameObject, but alucarj already showed you a nice Transform sorter.)
So, put all the enemies, in any order, into an array of Transforms. Then run the sort. A good thing about sorting arrays is everyone does it, not just Unity. So, you can just search for "C# array sort".
With only three, you could sort by hand, but still: sort first, then use. A 3-sort with ifs look like:
Transform Ca, Cb, Cc: // closest in order
Transform E1, E2, E3; // pretend these are the enemies, unsorted
float d1, d2, d3; // distance to each enemy
d1 = Vector3.magnitude(transform.position - E1.position), // same for E2 and E3
if(d1<d2 && d1<d3) {
Ca=E1;
if(d2<d3) { Cb=E2; Cc=E3;}
else { Cb=E3; Cc=E2; }
}
else { // E1 is not the closest
if(d2<d3) {
Ca=E2;
if(d1<d3) { Cb=E1; Cc=E3; }
else { Cb=E3; Cc=E2; }
}
else { // E3 is closest
Ca=E3;
if(d1<d2) { Cb=E1; Cc=E3; }
else { Cb=E2; Cc=E1; }
}
}
That's pretty ugly (and easy to get a single variable wrong.) A 4-sort is four times as long, which is why its easier to just learn arrays and sort that way.
Answer by AlucardJay · Mar 18, 2013 at 05:28 PM
OK, so this script actually does what you want. It is built so you can change the size of your enemies distance array to whatever size you like. The enemies distance array is hard-coded in the start, but as I say in the comments, you can use the Inspector to declare the size of your enemies distance array, and then fill in the values for the distances. The distances can be floats. If you use the inspector, comment out where the enemies array is sized and populated (I have put //==== at the beginning and end of this section) :
#pragma strict
public var enemies : float[]; // enemies in order [0, 1, 2, 3, 4, 5]
private var enemy : int[]; // reference to the position of the enemy
function Start()
{
// all this can be assigned in the Inspector
// set the size of the enemies array, then add the distances
// =====================
enemies = new float[6];
enemies[0] = 3.1;
enemies[1] = 5.2;
enemies[2] = 2.3;
enemies[3] = 7.4;
enemies[4] = 9.5;
enemies[5] = 4.6;
// =====================
// ----
// store enemy position from enemies array
enemy = new int[enemies.Length];
for ( var e : int = 0; e < enemy.Length; e ++ )
{
enemy[e] = e;
}
// ----
SortEnemiesByDistance();
DebugEnemyDistances();
}
function SortEnemiesByDistance()
{
// bubble-sort distances
for ( var e : int = 0; e < enemies.Length - 1; e ++ )
{
if ( enemies[e + 0] > enemies[e + 1] )
{
// sort distances
var tempStoreDistance : float = enemies[e];
enemies[e + 0] = enemies[e + 1];
enemies[e + 1] = tempStoreDistance;
// sort enemy index
var tempStoreEnemy : int = enemy[e];
enemy[e + 0] = enemy[e + 1];
enemy[e + 1] = tempStoreEnemy;
e = -1;
}
}
}
function DebugEnemyDistances()
{
for ( var e : int = 0; e < enemy.Length; e ++ )
{
Debug.Log( "enemy[" + enemy[e] + "] has a distance of " + enemies[e] );
}
}