Unity: more than one collider with different functionality in the same game object

2.8k views Asked by At

I am newbie.

I am trying to create a GameObject that has two colliders that may be triggered to make different functions. Lets say it is like a home in which if the player collider is within the enter collider of the home and you press space you can put things in the home, and if the player is within the second collider you can get out things from the home (something like that, the home acts as a queue in which you introduce things with player in the red spot and once they are ready you can take them out in the green spot)

The point is that it is not possible to have more that one trigger collider in the same gameobject because it is not possible to differentiate when one or the other is a trigger

how it's the procedure to make that

public Color normalColor;
     public Color playerOnTopColor;
     public GameObject receiveArea;
     private SpriteRenderer receiveArearRenderer;
 
     public float refresTime = 5f;
 
     public int totalCapacity = 5;
     public int currentCapacity = 0;
 
     public int totalReady = 5;
     public int currentReady = 0;
 
     public Text capacityText;
     public Text readyText;
 
     void Start()
     {
         receiveArearRenderer = receiveArea.GetComponent<SpriteRenderer>();
         receiveArearRenderer.color = normalColor;
         updateText();
         StartCoroutine(checkForQueue());
     }
 
 
     private void OnTriggerEnter2D(Collider2D collision)
     {
         receiveArearRenderer.color = playerOnTopColor;
     }
 
     private void OnTriggerExit2D(Collider2D collision)
     {
         receiveArearRenderer.color = normalColor;
     }

enter image description here

right now even if I enter in one collider or another its change the color of the bottom one because there is no possibility to check one the players enter in one and when in the other.

2

There are 2 answers

2
Thomas Finch On BEST ANSWER

You could (and probably should) not have both colliders on the same object. You could instead make the house one parent object, and two child objects each with their own colliders. If you want to keep all the code on the house object, you can still do that. Just create a relay component. Just make a new script that has this code in it:

 public UnityEvent OnTriggerEnter;
 public UnityEvent OnTriggerExit;

 private void OnTriggerEnter2D(Collider2D collision)
 {
     OnTriggerEnter?.Invoke();
 }

 private void OnTriggerExit2D(Collider2D collision)
 {
     OnTriggerExit?.Invoke();
 }

Put this script on each child object and then you can use the UnityEvents to run whatever methods you want from the Home script for each object individually.


Alternatively, if you really, really want to keep both colliders on the same object, then you could run manual collision checks. In this case, you'd remove the OnTriggerEnter/Exit methods and instead in FixedUpdate, do a Collider2D.OverlapCollider check for each collider. You'd want to assign each collider to a variable to be sure which collider you're doing what with.

0
Gabriel García Garrido On

I finally change the way of implementing this. I was wrong as Thomas said this is not the way of programming in unity, now it is a different child gameobject (with it owns colliders and script) and each child gameobject has its own functionality that calls the parent gameobject for functionally related with parent object purpose as show the numbers and so on... Thanks to @Thomas Finch