Hello all.
The game I’m making has mines in it. In order to set them off, a robot must be within range of it (in contact with the Trigger Circle Collider 2D) and have line of sight to the mine. This prevents a robot from triggering it from the other side of a wall. Below is the code I have used. (Please note, the end result will not be instant destruction, as is shown by the code. I just use that for testing as it makes it perfectly obvious whether the code is working or not!!)
For some reason, this code just doesn’t work. Funnily enough, when I change the tag to “Player”, being the tag that the player has, the code works perfectly and I can’t figure out why. I have checked that the layer that the “EnemyRobot” objects are on are included in the LOSLayerMask (as is the layer the player is on) but the enemy robots just don’t seem to trigger.
Can anyone please help me?
void OnTriggerEnter2D (Collider2D collision) {
if (collision.gameObject.tag == "EnemyRobot")
{
RaycastHit2D MineLOSTest = Physics2D.Linecast(transform.position, collision.gameObject.transform.position, LOSLayerMask);
if (MineLOSTest.collider != null)
{
if (MineLOSTest.collider.tag == "EnemyRobot")
{
Destroy(collision.gameObject);
}
}
}
}
void OnTriggerStay2D (Collider2D collision) {
if (collision.gameObject.tag == "EnemyRobot")
{
RaycastHit2D MineLOSTest = Physics2D.Linecast(transform.position, collision.gameObject.transform.position, LOSLayerMask);
if (MineLOSTest.collider != null)
{
if (MineLOSTest.collider.tag == "EnemyRobot")
{
Destroy(collision.gameObject);
}
}
}
}