- Home /
Question by
mitulmahish · Dec 02, 2021 at 03:09 PM ·
collisioncollidertriggerprogramming
How to trigger a specific collider ?
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?
Comment
Answer by visca_c · Dec 13, 2021 at 07:09 PM
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
}
}