Search Unity

Multiple triggers.

Discussion in '2D' started by westray26, Apr 22, 2020.

  1. westray26

    westray26

    Joined:
    Oct 15, 2016
    Posts:
    12
    I am making a Frogger type game at present.Obviously the logs are over the water(tagged as water and log),both with colliders set as trigger.How do I determine if I am over a log or over water and do something.At the moment the OnTriggerEnter event on the frog object catches both objects.
     
  2. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    As I recall, all the collision events give you the collider that was encountered. So you'll have use that to determine the type of object it is attached too. You have access via this collider to it's attached transform so you can likewise access the game object and all its components, so you should be able to identify what you're colliding with using that.
     
  3. westray26

    westray26

    Joined:
    Oct 15, 2016
    Posts:
    12
    In the Trigger event if I do an if/else statement to choose an object with tag it is still sensing all the objects that have entered the trigger.
     
  4. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    You don't look for tag, you look for a specific type of attachment.

    Like for me, in my game I have a grab command which looks like this:

    Code (CSharp):
    1. Collider2D[] potentialPickups = Physics2D.OverlapCircleAll(GrabPosition.position, GrabRadius, GrabLayers);
    2.         if (potentialPickups != null && potentialPickups.Length > 0)
    3.         {
    4.             Collider2D firstPotentialPickup = potentialPickups[0];
    5.             ResourceNodeController rnc = firstPotentialPickup.GetComponent<ResourceNodeController>();
    6.             if (rnc != null)
    7.             {
    8.                 ResourceNode rn = GameCore.Current.Resources[rnc.ResourceNodeID];
    9.                 if (rn.HarvestHealth == 0)
    10.                 {
    11.                     InventoryContainer results = rnc.GrabResource();
    12.                     if (results != null)
    13.                     {
    14.                         PlayerData.Current.Inventory.MergeContainers(results);
    15.                         if (!results.IsEmpty())
    16.                             PlayerData.Current.ToolBar.MergeContainers(results);
    17.                         if (!results.IsEmpty())
    18.                         {
    19.                             GameObject go = Instantiate(GameResources.Current.LootBagPrefab, transform.position, Quaternion.identity);
    20.                             InventoryContainerController lc = go.GetComponent<InventoryContainerController>();
    21.                             lc.InventoryContainer_var = results;
    22.                             lc.Initialize();
    23.                         }
    24.                     }
    25.                 }
    26.             }
    27.             else
    28.             {
    29.                 IInteractableElement iie = firstPotentialPickup.GetComponent<IInteractableElement>();
    30.                 //InventoryContainerController lc = firstPotentialPickup.GetComponent<InventoryContainerController>();
    31.                 if (iie != null)
    32.                 {
    33.                     if (grabAll && iie is InventoryContainerController icc)
    34.                     {
    35.                         PlayerData.Current.Inventory.MergeContainers(icc.InventoryContainer_var);
    36.                         if (!icc.InventoryContainer_var.IsEmpty())
    37.                             PlayerData.Current.ToolBar.MergeContainers(icc.InventoryContainer_var);
    38.                     }
    39.                     iie.DisplayUI();
    40.                 }
    41.             }
    42.         }
    As you can see it checks what component is on the object. now this is a user driven command rather than a collision event, but it should be similar.

    Edit: Just added the code tag as requested
     
    Last edited: Apr 23, 2020
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,487
    Thanks for helping. Can you please use code-tags when posting code so it's easier to read.

    Thanks.
     
  6. westray26

    westray26

    Joined:
    Oct 15, 2016
    Posts:
    12
    After some searching I found this way of doing it using Physics2D.OverlapCircle.It works like a charm.I set the Z of the Logs to 1 and on the Water to 2.The function returns whichever Collider has the nearest Z position to the caster.
    Code (CSharp):
    1.   private void CheckIfColliding()
    2.     {
    3.  
    4.         if (HasMoved)
    5.         {
    6.             Collider2D hit;
    7.             hit = Physics2D.OverlapCircle(transform.position, 0.2f);
    8.             if (hit != null)
    9.             {
    10.  
    11.  
    12.                 switch (hit.tag)
    13.                 {
    14.                     case "Log":
    15.                         transform.SetParent(hit.transform);
    16.                         //CurrentLog = hit.gameObject;
    17.                         break;
    18.  
    19.                     case "Water":
    20.  
    21.                         Destroy(gameObject);
    22.                         break;
    23.  
    24.                     case "Safe":
    25.  
    26.  
    27.                         Instantiate(Frog_Home, hit.transform.position, hit.transform.rotation);
    28.  
    29.                         AudioSource.PlayOneShot(Home);
    30.                         Invoke("RestartLevel", 1);
    31.                         Frog_Sprite.enabled = false;
    32.                         ; Debug.Log("Frog is Home");
    33.                         break;
    34.  
    35.  
    36.  
    37.                 }
    38.  
    39.             }
    40.  
    41.         }
    42.