Collider fires consistently, but Trigger does not?
My player object collides properly but the trigger for the same object (placed on a child) does not work most of the time.
To preface I want a collider for the Player for physics, and a trigger on it for detection, and use both of these in scripts. As recommended by many users on questions with the same requirements, I've set up the GameObject hierarchy as such:
- Player (has a RigidBody2D)
ColliderZone (has a Circle Collider 2D of radius 0.165, no RigidBody2D)
TriggerZone (has a Circle Collider 2D of radius 0.4, with 'Is Trigger' checked, no RigidBody2D)
There is a script attached to ColliderZone which sends a message to the console when OnCollisionEnter2D fires, which works perfectly, and the player bounces off walls. However a similar script in the TriggerZone object has OnTriggerEnter2D, which does not fire as expected. I have static walls that the player may collide with (they have box colliders but no rigidbodies) and the player does do so, but the trigger zone which is a larger radius than the collision zone, does not cause OnTriggerEnter2D to fire consistently when it overlaps with the wall's collider. In some cases it does, but in many the OnTriggerEnter2D event doesn't seem to occur. For example, if I set some particles to emit when the player enters within 0.4 units distance of the wall (i.e, when the trigger overlaps with the wall), it only fires some of the time, however, particles set to emit during a physics collision with the wall causes them to emit every time.
I'm not sure what's causing this behaviour, so I'd appreciate any help.
ColliderZoneScript.cs
using UnityEngine;
public class ColliderZoneScript : MonoBehaviour {
private void OnCollisionEnter2D(Collision2D collision)
{
// Functions without issue.
Debug.Log("Collision Registered);
}
}
TriggerZoneScript.cs
using UnityEngine;
public class TriggerZoneScript : MonoBehaviour {
private void OnCollisionEnter2D(Collision2D collision)
{
// 40% of the time, it works every time.
Debug.Log("Trigger Registered);
}
}
Your answer
Follow this Question
Related Questions
2D RigidBody slightly penetrates objects causing a bounce effect. 3 Answers
C# OnCollisionEnter not Triggering If Statement 1 Answer
Destroy object on collision not working 1 Answer
Cannot detect trigger 2 Answers
Why/How 2d tower of blocks collapse? 0 Answers