trigger inconsistent behaviour?

98140-nocollision.png

Hi,
I have a strange problem. My character 2D collision sometimes does not fire the OnTriggerEnter2D method when entering a trigger collision for 1 frame. This happens about 10% of the time, when performing the exact same movement each time.

My supplied image shows the position of the character’s collision box for each frame, and you can see where it is overlapping the trapezoid trigger.

Some more info:

  • There is nowhere in my script where I disable the collision on the character
  • The world positions of the character at each step are replicated exactly each time the jump is performed.
  • A new coroutine is run every frame to draw the position at that frame for 2 seconds
  • Rather than move my character with Time.deltaTime, I use a fixed value (1/60) to ensure consistent positions from identical movement.

As far as I can tell, sometimes it just “doesn’t work”.

Any help would be appreciated! Here’s my ontrigger method (very simple),

void OnTriggerEnter2D(Collider2D collider)
    {
        Debug.Log("Hit trigger");
    }

(Update)

So I tried having my character simply run into a trigger. Most of the time it correctly ‘dies’ when entering the trigger, but now and again it will be inside the trigger for three extra frames before detecting that it is inside the triggger. What gives? My Physics2D values are all default BTW.

Left image shows the character ‘dying’ after being inside the red trigger for 1 frame, right image shows the delayed trigger. (blue boxes are just death particles)

(Update2)

This is the coroutine that runs every frame, drawing the position during that frame for 2 seconds,

IEnumerator DebugTemporarilyDrawPosition()
    {
        float t = 2f;
        Color c = DebugDrawColor;
        Vector3 position = transform.position;
        while (t > 0)
        {
            float xo = _baseScale.x * 0.5f;
            float yo = _baseScale.y * 0.5f;
            Debug.DrawLine(new Vector3(position.x - xo, position.y - yo, 0), new Vector3(position.x - xo, position.y + yo, 0), c);
            Debug.DrawLine(new Vector3(position.x + xo, position.y - yo, 0), new Vector3(position.x + xo, position.y + yo, 0), c);
            Debug.DrawLine(new Vector3(position.x - xo, position.y - yo, 0), new Vector3(position.x + xo, position.y - yo, 0), c);
            Debug.DrawLine(new Vector3(position.x - xo, position.y + yo, 0), new Vector3(position.x + xo, position.y + yo, 0), c);
            yield return null;
            t -= FixedTime.delta;
        }
    }

Add a bool and move your code to OnTriggerStay (if false, make it true and execute collision). Trigger enter fails sometimes as you noticed