Search Unity

Looking for a way to connect two specific GameObjects...

Discussion in 'Scripting' started by KippySmith, Sep 21, 2018.

  1. KippySmith

    KippySmith

    Joined:
    Nov 29, 2017
    Posts:
    1
    Hey, thanks for reading. So I'm basically trying to cause two game objects to interact with one another, the two objects being: an AI patron, and a stool.

    The idea, is that when the stool is not occupied, it searches for a patron from a list of patrons in the scene. When it selects a random one, the patron is then instructed to move toward the stool that selects it.

    This works fine, but the issue that I run into, is that there are multiple stools all searching for a patron at once. They all latch onto the first patron in the scene, and ignore subsequent patrons. Previously, I had it that the patrons would search for empty stools instead, but this seemed to make even less sense because then there were 10 patrons looking for one open stool, rather than one open stool looking for one specific patron.

    Overall, what I'm trying to do, is find some way for the stool to say "Here is the patron I've selected", then the patron says "This is the stool that has selected me", and then the patron moves to that stool, and every other stool now understands that that specific patron has already been selected for seating. Is there a way to do this simply? This is what I have, but it doesn't function correctly.

    Code (CSharp):
    1.     public List<GameObject> patrons;
    2.     public GameObject selectpatron;
    3.     public Transform stoollocation;
    4.     public GameObject thisstool;
    5.     public float speed = 1f;
    6.     public Rigidbody2D patronrb2d;
    7.     public int randomint;
    8.     public bool patronused;
    9.  
    10.     public void Start()
    11.     {
    12.         thisstool = gameObject;
    13.     }
    14.  
    15.     public void Update()
    16.     {
    17.         patrons = new List<GameObject>();
    18.  
    19.  
    20.         foreach(GameObject patron in GameObject.FindGameObjectsWithTag("patron"))
    21.         {
    22.             patrons.Add(patron);
    23.         }
    24.  
    25.  
    26.  
    27.         if(selectpatron == null)
    28.         {
    29.             PatronSelected();
    30.         }
    31.  
    32.         if(patronused = true)
    33.         {
    34.             PatronSelected();
    35.         }
    36.            
    37.     }
    38.  
    39.  
    40.     void OnTriggerEnter2D(Collider2D other)
    41.     {
    42.         transform.gameObject.tag = "stoolOccupied";
    43.     }
    44.     private void OnTriggerExit2D(Collider2D other)
    45.     {
    46.         transform.gameObject.tag = "Stool";
    47.     }
    48.  
    49.     public void PatronSelected()
    50.     {
    51.         randomint = Random.Range(0, patrons.Count);
    52.         selectpatron = patrons[randomint];
    53.         patronused = selectpatron.GetComponent<PatronMoveScr>().selected;
    54.         selectpatron.GetComponent<PatronMoveScr>().Selected();
    55.         patrons.RemoveAt(randomint);
    56.         patronrb2d = selectpatron.GetComponent<Rigidbody2D>();
    57.         transform.gameObject.tag = "stoolOccupied";
    58.         Vector2 velocity = new Vector2((transform.position.x - selectpatron.transform.position.x) * speed, (transform.position.y - selectpatron.transform.position.y) * speed);
    59.         patronrb2d.velocity = velocity;
    60.     }
    61.        
    62. }
    Code (CSharp):
    1.     private Rigidbody2D rbpatron2d;
    2.     public float speed;
    3.     private Transform target;
    4.     public int thirst;
    5.     private GameObject Stool;
    6.     public List<GameObject> stools;
    7.     private bool seated;
    8.     public GameObject stoolselection;
    9.     public bool selected = false;
    10.  
    11.  
    12.     // Use this for initialization
    13.     void Awake ()
    14.     {
    15.         thirst = Random.Range(10, 100);
    16.         seated = false;
    17.         selected = false;
    18.         speed = 1;
    19.     }
    20.  
    21.     void Start ()
    22.     {
    23.         rbpatron2d = GetComponent<Rigidbody2D>();
    24.     }
    25.    
    26.     // Update is called once per frame
    27.     void Update ()
    28.     {
    29.         stools.AddRange(GameObject.FindGameObjectsWithTag("Stool"));
    30.         thirst = gameObject.GetComponent<thirstScr>().Thirst;
    31.      
    32.      //If patron is near stool disable rigidbody to prevent other patrons from pushing them off stool
    33.         if (Vector2.Distance(transform.position, stoolselection.transform.position) < 0.5f)
    34.         {
    35.             seated = true;
    36.         }
    37.  
    38.         if(seated && thirst > 0)
    39.         {
    40.             //Deactivate ridgidbody
    41.             rbpatron2d.isKinematic = true;
    42.         }
    43.         else
    44.         {
    45.             //Reactive ridgidbody
    46.             rbpatron2d.isKinematic = false;
    47.         }
    48.            
    49.  
    50.     }
    51.  
    52.     void FixedUpdate ()
    53.     {
    54.  
    55.     }
    56.  
    57.     public void Selected()
    58.     {
    59.         selected = true;
    60.     }
    61.        
    62.  
    63. }
     
  2. TimmyTheTerrible

    TimmyTheTerrible

    Joined:
    Feb 18, 2017
    Posts:
    186
    What I did was my NPCs was when they would spawn, they would use an overlap sphere to search for colliders set to a specific npc action layer.

    It would then get the ActionTree component on the colliders it found. It would then check to see if the ActionTrees reference to an NPC was null. If it wasn't, then some other NPC was already undertaking this action, and the NPC loop to test the next one. If it was null, then assign this npc to the ActionTree and make sure to set the ActionTrees actor reference to this NPC.

    Hope that helps!