I am working on a top-down infinite scroller where the player drives up and avoids traffic that is spawned on random locations on the road. The player has a box collider (tagged “PlayerCheck”) that prevents traffic from spawning within view of the player camera. Each instantiated prefab of traffic has a capsule collider (tagged “Traffic”) that is intended to prevent traffic from spawning on top of existing traffic.
The road has an array of spawn points. The road also has a box collider that sits above the spawn points. The road has a spawn manager script (see below). The trigger colliders determine the values of the two boolean variables. If those values are both true, a spawn function is called (NorthSpawnMechanism).
The main issue is that for some reason, the PlayerCheck collider successfully turns off the spawn function, but the Traffic collider does not. The Traffic collider even correctly prints the Traffic Debug.Log messages when Traffic passes through the road spawn point collider. Why is one of my trigger colliders (PlayerCheck) preventing the call of a function while the other (Traffic) does not?
I have tried removing the PlayerCheck and tested the Traffic trigger by itself and still no luck. Thanks in advance for any assistance (first time coder here)!
bool spawnCarsPlayer = true;
bool spawnCarsTraffic = true;
void Update()
{
if (spawnCarsPlayer == true && spawnCarsTraffic == true)
{
NorthSpawnMechanism();
}
}
void NorthSpawnMechanism()
{
if (Time.time > nextSpawnTime)
{
nextSpawnTime = Time.time + secondsBetweenSpawns;
randomNorthSpawnPoint = Random.Range(0, northSpawnPoints.Length);
Instantiate(NorthTrafficPrefab, northSpawnPoints[randomNorthSpawnPoint].position, Quaternion.identity);
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "PlayerCheck")
{
spawnCarsPlayer = false;
Debug.Log("The player can see the spawn point.");
}
if (other.tag == "Traffic")
{
spawnCarsTraffic = false;
Debug.Log("There is traffc in the way.");
}
}
private void OnTriggerExit2D(Collider2D other)
{
if (other.tag == "PlayerCheck")
{
spawnCarsPlayer = true;
Debug.Log("The player has moved on.");
}
if (other.tag == "Traffic")
{
spawnCarsTraffic = true;
Debug.Log("The traffic has moved on.");
}
}