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.
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.
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.
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): Collider2D[] potentialPickups = Physics2D.OverlapCircleAll(GrabPosition.position, GrabRadius, GrabLayers); if (potentialPickups != null && potentialPickups.Length > 0) { Collider2D firstPotentialPickup = potentialPickups[0]; ResourceNodeController rnc = firstPotentialPickup.GetComponent<ResourceNodeController>(); if (rnc != null) { ResourceNode rn = GameCore.Current.Resources[rnc.ResourceNodeID]; if (rn.HarvestHealth == 0) { InventoryContainer results = rnc.GrabResource(); if (results != null) { PlayerData.Current.Inventory.MergeContainers(results); if (!results.IsEmpty()) PlayerData.Current.ToolBar.MergeContainers(results); if (!results.IsEmpty()) { GameObject go = Instantiate(GameResources.Current.LootBagPrefab, transform.position, Quaternion.identity); InventoryContainerController lc = go.GetComponent<InventoryContainerController>(); lc.InventoryContainer_var = results; lc.Initialize(); } } } } else { IInteractableElement iie = firstPotentialPickup.GetComponent<IInteractableElement>(); //InventoryContainerController lc = firstPotentialPickup.GetComponent<InventoryContainerController>(); if (iie != null) { if (grabAll && iie is InventoryContainerController icc) { PlayerData.Current.Inventory.MergeContainers(icc.InventoryContainer_var); if (!icc.InventoryContainer_var.IsEmpty()) PlayerData.Current.ToolBar.MergeContainers(icc.InventoryContainer_var); } iie.DisplayUI(); } } } 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
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): private void CheckIfColliding() { if (HasMoved) { Collider2D hit; hit = Physics2D.OverlapCircle(transform.position, 0.2f); if (hit != null) { switch (hit.tag) { case "Log": transform.SetParent(hit.transform); //CurrentLog = hit.gameObject; break; case "Water": Destroy(gameObject); break; case "Safe": Instantiate(Frog_Home, hit.transform.position, hit.transform.rotation); AudioSource.PlayOneShot(Home); Invoke("RestartLevel", 1); Frog_Sprite.enabled = false; ; Debug.Log("Frog is Home"); break; } } }