Hi, I’m new to Unity and C#.
I’m trying to use “OnTrigger” for detecting if the player enters the trigger Polygon Collider of the Enemy . But, It’s checking for Box Collider .
Please help
private void OnTriggerEnter2D(Collider2D other) {
Health playerHealth = other.GetComponent<Health>();
if(playerHealth != null) {
playerHealth.LoseLife();
}
}
Should I use OnCollision2d() ? If yes, then how to specify the colliders?
visca_c
December 13, 2021, 7:09pm
2
You might not want to trigger specific collider type (for good structure), but you can make collider child object with specific component and attach them to your player.
For example:
On Your Player Scene Hierarchy:
Player
-----Collider1 GameObject (Attached Script: Collider1Behaviour)
-----Collider2 GameObject (Attached Script: Collider1Behaviour)
On Enemy Collider Script:
private void OnTriggerEnter2D(Collider2D other)
{
if(other.GetComponent<Collider1Behaviour>()!=null)
{
//Do Something
}
}