Hi. I have a parent object A that has a non-trigger type boxCollider. Object A has 4 children named B, C, D, E that have CircleCollider of type trigger. So I need an external object X of trigger type to enter and be detected by A’s collider without colliders B, C, D, E detecting it.
Thank you
,Hello
I have a parent object named A that has a non-trigger type boxCollider, plus A has 4 children named B, C, D, E who each have a trigger type CircleCollider. I need an external trigger type CircleCollider object to collide with object A without being triggered by B, C, D, E
Thank you
By calling GetComponents(), you’ll get a list of your circle colliders in the order they’re attached to the game object.
private void Awake() {
colA = GetComponent<BoxCollider2D>();
var colliders = GetComponents<CircleCollider2D>();
// Assuming this is the order you've put them in on your game object:
colB = colliders[0];
colC = colliders[1];
colD = colliders[2];
colE = colliders[3]
}
That will help you to track which of the 4 trigger colliders is which, but your question is more about the non-trigger box collider. OnCollisionEnter2D won’t fire on your player when the box collider hits your coin object’s trigger collider. However, OnTriggerEnter2D will fire on your coin object if the trigger collider attached to your coin object hits your player’s non-trigger collider. You can detect it by adding this to your coin object:
private void OnTriggerEnter2D(Collider2D col) {
// cast the other collider to box collider to weed out your circle colliders.
// If the cast fails, it's not the collider you're looking for.
BoxCollider2D boxCol = col as BoxCollider2D;
if (boxCol != null) {
// Add a tag for your player at the top of the inspector,
// and assign it to the parent player object.
// You can use it to identify the player's colliders. (careful, tags are case-sensitive).
if (boxCol.CompareTag("Player")) {
// Code for collecting your coin.
}
}
}
Each collider will need to be on a separate object. just create 4 new empty objects, and attach a collider to each.
@Victor146 You only need to make one child object of your parent with all 4 circle colliders attached to it & activate isTrigger on them.
Then change the offset of each circle collider according to your desired position.