Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Make one bot's destination not the same as another's

Discussion in 'Scripting' started by AdrianMontalban, Oct 12, 2023.

  1. AdrianMontalban

    AdrianMontalban

    Joined:
    Jul 17, 2023
    Posts:
    1
    Hello, I am new to Unity and I am trying to make a bot with NavMeshAgent have a destination (Chair) and that while it is sitting in it no other bot can sit in the same place as the first bot. can anybody help me?

    Code (CSharp):
    1.     [Header("POINTS")]
    2.     public GameObject Point1;
    3.     public GameObject Point2;
    4.     public GameObject Point3;
    5.     public GameObject Point4;
    6.     public GameObject Point5;
    7.     public GameObject Point6;
    8.  
    9.    
    10.     [Header("OCCUPIED POINTS")]
    11.     public bool Point1Ocupado = false;
    12.     public bool Point2Ocupado = false;
    13.     public bool Point3Ocupado = false;
    14.     public bool Point4Ocupado = false;
    15.     public bool Point5Ocupado = false;
    16.     public bool Point6Ocupado = false;
    17.  
    18.     private GameObject Posicion = null;
    19.  
    20.         //Bot destination
    21.         destination = null;
    22.         int RandomNumber;
    23.  
    24.        IEnumerator Destination()
    25.       {
    26.        
    27.   while (destination == null)
    28.         {
    29.             RandomNumber = Random.Range(1, 7);
    30.  
    31.             if(RandomNumber == 1 && Point1Ocupado == false)
    32.             {
    33.                 destination = Point1;
    34.             }
    35.  
    36.             if (RandomNumber == 2 && Point2Ocupado == false)
    37.             {
    38.                 destination = Point2;
    39.             }
    40.  
    41.             if (RandomNumber == 3 && Point3Ocupado == false)
    42.             {
    43.                 destination = Point3;
    44.             }
    45.  
    46.             if (RandomNumber == 4 && Point4Ocupado == false)
    47.             {
    48.                 destination = Point4;
    49.             }
    50.  
    51.             if (RandomNumber == 5 && Point5Ocupado == false)
    52.             {
    53.                 destination = Point5;
    54.             }
    55.  
    56.             if (RandomNumber == 6 && Point6Ocupado == false)
    57.             {
    58.                 destination = Point6;
    59.             }
    60.         }
    61.  
    62.         NavMesh.destination = destination.transform.position;
    63. }
    64.    
    65.     private void OnTriggerEnter(Collider other)
    66.     {
    67.      
    68.         //Method for know if the chair is occupied
    69.  
    70.         if (other.tag == "Mesa1")
    71.         {
    72.             Point1Ocupado = true;
    73.         }
    74.  
    75.         if (other.tag == "Mesa2")
    76.         {
    77.             Point2Ocupado = true;
    78.         }
    79.  
    80.         if (other.tag == "Mesa3")
    81.         {
    82.             Point3Ocupado = true;
    83.         }
    84.  
    85.         if (other.tag == "Mesa4")
    86.         {
    87.             Point4Ocupado = true;
    88.         }
    89.  
    90.         if (other.tag == "Mesa5")
    91.         {
    92.             Point5Ocupado = true;
    93.         }
    94.  
    95.         if (other.tag == "Mesa6")
    96.         {
    97.             Point6Ocupado = true;
    98.         }
    99.  
    100.     }
    101.  
    102.    
    103.  
    104. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,298
    You need to write a destination manager that tracks who is going for what destination.

    The API might look like "give me a destination" and be prepared to receive "none available" for instance.

    Another call might be "I have reached (or maybe departed) the destination you gave me earlier, release that for others to go to."
     
    Nad_B likes this.
  3. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    761
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.AI;
    3.  
    4. public class MusicalChairs : MonoBehaviour
    5. {
    6.     public GameObject[] destinations; // drag multiple destinations into this array
    7.     public LayerMask playersLayer; // all bots go on this layer
    8.     public NavMeshAgent agent;
    9.  
    10.     int d;
    11.  
    12.     void Update()
    13.     {
    14.         int ourLayer=gameObject.layer;
    15.         gameObject.layer = 2; // Ignore raycast (..or CheckSphere)
    16.         if (Physics.CheckSphere(destinations[d].transform.position, 3, playersLayer)==false) // nobody near desination d ?
    17.             agent.destination = destinations[d].transform.position;
    18.         else
    19.             d=Random.Range(0,destinations.Length); // else pick another possible destination from the array
    20.         gameObject.layer=ourLayer;
    21.     }
    22. }
    23.