Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Multiple agents and multiple destinations?

Discussion in 'Navigation' started by Kouseii, May 30, 2022.

  1. Kouseii

    Kouseii

    Joined:
    Jan 2, 2019
    Posts:
    9
    Hello, I have multiple agents and multiple destinations. These destinations will randomly selected but if one of destination is already selected by other agent, it will reselect another unselected destination. How can I make this?

    Example: There is a town, 4 customers coming and they are sitting in different chairs at the table. Their destinations are set from the beginning I think? They do not sit on top of each other.
     
  2. MarekUnity

    MarekUnity

    Unity Technologies

    Joined:
    Jan 6, 2017
    Posts:
    203
    You could get a list of points and shuffle it and then assign it to each agent.
    Code (CSharp):
    1. destinations.OrderBy(e => Random.value).ToList()
    In case you don't want to use Linq expressions (OrderBy) you could shuffle it yourself:

    Code (CSharp):
    1. var destinationsNum = destinations.Count;
    2. for (var i = 0; i < destinationsNum; i++)
    3. {
    4.     var j = Random.Range(i, destinationsNum);
    5.     (destinations[i], destinations[j]) = (destinations[j], destinations[i]);
    6. }
    Hope this helps!
     
    Kouseii likes this.
  3. Inxentas

    Inxentas

    Joined:
    Jan 15, 2020
    Posts:
    277
    Another method would be to de-couple the logic by creating a method other entities can ask your Table object.

    For instance, make it have a GetFreeChair() method that always returns a free chair, or NULL if none are available. That way other entities can "grab a chair" without needing to distribute the points among a group of entities beforehand, and you'd only have to think about what needs to happen when there are no more chairs available at the table, and in what order they are "served" to your entities.

    Linq expressions (OrderBy) are powerful though, and one of my favourite ways to sort data.
     
    MarekUnity likes this.