- Home /
Have determined it is a fault with the OnTrigger methods, and resorted to checking collisions within the Update() method instead.
trigger inconsistent behaviour?
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;
}
}
Answer by Vollmondum · Jul 22, 2017 at 03:23 PM
Add a bool and move your code to OnTriggerStay (if false, make it true and execute collision). Trigger enter fails sometimes as you noticed
so I already have an '_alive' bool that gets set to false when entering the trigger, and bails out of the trigger method if not alive, so I simply changed it from OnTriggerEnter2D to OnTriggerStay2D - and the results are still inconsistent as well as taking 4 frames longer $$anonymous$$imum (so 4 $$anonymous$$imum detection time, 7 frames maximum), so unfortunately this solution actually gives worse results.
Perhaps the only way I can ensure reliable results is to scrap using OnTrigger methods entirely and put in an overlaps test into the Update() method, which is a shame as I'd have expected OnTrigger to do its job.
I appreciate your help besides
Why don't you create 3D colliders ins$$anonymous$$d? In fact, since OnStay is taking 4 frames longer, you might wanna check if some method is being continuously called, that might be a cause for the delay. Use "print" statement to check if your main lines are only executed once per necessity. I mean people overestimate Update function hassling it all the time. The only thing that should really be constantly Updated are movement and time-dependent counters, like countdown.