I am currently working on a rhythm game for myself and my friends, and I’ve run into an issue where I have multiple overlapping colliders on one object and I would like to be able to determine which of those colliders are being touched at one time.
To be more specific, I have “beats” coming and colliding with a “beat catcher.” If the appropriate button is pressed when an OnTriggerStay2D occurs, then I want to be able to find the smallest collider of the three on the beat catcher that is currently being touched. Based on that, the player will get higher points for better timing.
Here is my current code
using System.Collections;
using System.Collections.Generic;
using UnityStandardAssets.CrossPlatformInput;
using UnityEngine;
public class BeatCatcher : MonoBehaviour
{
//Each of the colliders on the beat catcher
[SerializeField] BoxCollider2D greatZone;
[SerializeField] BoxCollider2D okZone;
[SerializeField] BoxCollider2D badZone;
bool leftBongo;
bool rightBongo;
//This is triggered if the beat is in the bad zone because the other zones are
//inside the bad zone. It would be better to have the collision check inside an
//update function so it doesn't get called three times on a frame, once per collider
private void OnTriggerStay2D(Collider2D beat)
{
//Get current Input
leftBongo = CrossPlatformInputManager.GetButtonDown("P1 Left Bongo");
rightBongo = CrossPlatformInputManager.GetButtonDown("P1 Right Bongo");
//I'm not happy the way this is done, but I currently can't find another
//way to determine if the colliding beat is the same as my left beat prefab
if (beat.gameObject.name == "Left Beat(Clone)" && leftBongo)
{
//if (the beat is colliding with the greatZone collider) say great and give lots of points
//else if (the beat is in the okZone collider) say ok and give some points
//else say bad and give a few points
Destroy(beat.gameObject);
}
}
}