Sunday, September 11, 2022
HomeGame Developmentunity - Sort an inventory of objects by distance?

unity – Sort an inventory of objects by distance?


Today I acquired problem with a script we solved right here. OK, Here if enemy enter participant ring, he “enemy” will get inside participant checklist. Now, if group of enemies get inside this the ring I must type them by distance from participant. The first one within the checklist would be the closest enemy from participant, and so forth. If one die, new enemy will add to checklist that nearest/closest to participant. Will my GameObject checklist permit me to do that ? or I must make new Vector3 checklist ? I’m utilizing System.Linq.

    public List<GameObject> enemiesInRange = new List<GameObject>();
    public int hox;

// Update is known as as soon as per body
void Update()
{

    hox = enemiesInRange.Count;
    if (hox == 3)
        print("Frist enemy close to you is " + enemiesInRange[0] + ", " + enemiesInRange[1] + ", " + enemiesInRange[2]);
    //   enemiesInRange[0].GetComponent<enemypath>().enemey_checker = true;
    //   enemiesInRange[1].GetComponent<enemypath>().enemey_checker = true;
    //   enemiesInRange[2].GetComponent<enemypath>().enemey_checker = true;

}


void OnTriggerEnter(Collider different)
{
    if (different.CompareTag("enemy") && hox < 3)
        enemiesInRange.Add(different.attachedRigidbody.gameObject);
}

void OnTriggerExit(Collider different)
{
    if (different.CompareTag("enemy"))
        enemiesInRange.Remove(different.attachedRigidbody.gameObject);
}

I actually hope to resolve this problem and end from this level.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments