- Home /
Collision Detection - Strange issues! Working when it shouldnt, not working when it should!
Good Morning Guys and Girls.
Having some really weird things happening with a simple 2D box collider set to trigger.
I have an NPC (Set up with box collider and 2D rigidbody) walking into a box collider (Set as trigger)
The player has been set up with the tag "ScavManOne" and I am using the following script:
public class DinerLightTriggerScript : MonoBehaviour {
public bool dark = false;
public float darkness = 1;
void OnTriggerEnter2D (Collider2D other)
{
if (gameObject.tag == "ScavManOne")
{
dark = true;
ScavOneManager.ScavOneMan.scavOneMentalStateLevel -= darkness * Time.deltaTime;
}
}
void OnTriggerExit2D (Collider2D other)
{
if (gameObject.tag == "ScavManOne")
{
dark = false;
}
}
}
Now the problem im having is that it simply doesnt work, the bool does not get set to true when he enters and doesnt go to false when he leaves. so, i decided to conduct a test just using an white box sprite and dropping it into the same trigger (The box was set up with the tag "box") and I used the following script:
public class DinerLightTriggerScript : MonoBehaviour {
public bool dark = false;
public float darkness = 1;
void OnTriggerEnter2D (Collider2D other)
{
if (gameObject.tag == "box")
{
dark = true;
ScavOneManager.ScavOneMan.scavOneMentalStateLevel -= darkness * Time.deltaTime;
}
}
void OnTriggerExit2D (Collider2D other)
{
if (gameObject.tag == "box")
{
dark = false;
}
}
}
And it worked just fine, the box goes in, the bool is set to true, the box gets moved the bool is set to false as intended, but then I noticed that when the NPC moves into the trigger he now sets it on and off.
The problem that I have is that the same NPC from the first example also triggers the bool ??
So to sum up: Correctly tagged character (scavManOne) doesnt trigger the script Correctly tagged box (box) does trigger the script but so does the NPC (ScavManOne)
Im so confused!
Answer by Masterio · Mar 23, 2016 at 01:51 PM
you should compare with the other.gameObject.tag
Answer by perrinostudios · Mar 23, 2016 at 02:09 PM
Masterio...
That worked like a charm! Thanks mate!