How to check if Player collides with trigger?
Hi! I've looked across the internet, but no answer has worked! I'm trying to make an enemy script with a Field of View that is a circle in a 2D scroller game, and I need it to be a trigger that triggers when the player collides with it to change a boolean to activate the enemy. The trigger works perfectly, but whenever I add an if statement to check if it's the player and not anything else, it doesn't do anything
void OnTriggerEnter2D (Collider2D other) { if (other.tag == "Player") { Debug.Log ("We been triggered"); } }
Answer by unity_BUJQdslYm7EuFQ · Jul 15, 2021 at 01:19 PM
Check, has your player tag, exactly matching with the tag in your code. If you want, use the CompareTag function. It is better than just comparing tags with == operator.
other.CompareTag("Player");
If written above desision does not help, check availability of trigger on your player.
Answer by tomosbach · Jul 16, 2021 at 12:15 PM
It's also worth checking that you:
Have a collider on your player
Have a collider on the other object
The colliders are both set to Trigger - I can't quite remember off the top of my head whether OnTrigger needs the player's collider to be set to trigger, or the other object, but if you set both to be a trigger and the problme goes, you can hopefully work it out from there :)
Your answer
Follow this Question
Related Questions
(C#) Bullet Damage , OnCollisionTrigger2D not working,(C#) Bullet Damage, Trigger not working. 2 Answers
How to Deactivate a Trigger collider on a Specific GameObject 2 Answers
How to check if player collides with trigger? 0 Answers
Unity 2D: Glitchy Collision/Triggers Problem 0 Answers
Collision Triggers at Random 1 Answer