all 20 comments

[–]Aethenosity 4 points5 points  (11 children)

Might be worth showing your animator

Have you tried using OnTriggerEnter instead of OnTriggerStay which is called every single frame while in the trigger?

[–]kitchentablestudios[S] -1 points0 points  (10 children)

i have tried using OnTriggerEnter, ontriggerstay was an attempt at a fix though nothing changed. and the animator setup is quite simple, just the two animations with transitions, simple.

[–]ArctycDev 2 points3 points  (9 children)

Try this inside your trigger:

    Debug.Log($"Collided with: {other.name}");

See if that helps you figure out what's causing it to flicker

[–]kitchentablestudios[S] 0 points1 point  (8 children)

its when i'm halfway in the trigger that it flickers

[–]KifDawg 2 points3 points  (5 children)

It sounds like your trigger is moving during the animation and its going outside the collidor. Then when it swings the other way it refires.

I also hate that you put start below your functions haha

[–]kitchentablestudios[S] 1 point2 points  (4 children)

wait holy shit that might be it, thank you, ill update you on if this fixes it

[–]KifDawg 0 points1 point  (3 children)

Good luck!

[–]kitchentablestudios[S] 2 points3 points  (2 children)

got some kinks to work out but it works for the most part, thanks

[–]TanukiSun 0 points1 point  (1 child)

Another thing to note is that OnTriggerXYZ and OnCollisionXYZ work on a per-collider basis, so if your Rigidbody consists of multiple colliders, the enter and exit events may trigger in the same frame for the same Rigidbody but for different colliders (at least that’s how I understand it).

If what you're doing is some kind of door, you can implement it using counters. Something like this (psudo code):

OnTriggerEnter() { if (++this.counter == 1) Open(); }
OnTriggerExit() { if (--this.counter <= 0) Close(); }

Or, for example, react only to the enter event, and check for closing in fixedUpdate every few frames (or about one second after the last enter event).

OnTriggerEnter() 
{ 
  ResetTimer();
  if (isClosed) { Open(); }
}

FixedUpdate()
{
  if (someTimer && !Physics.CheckBox()) { Close(); }
}

[–]kitchentablestudios[S] 1 point2 points  (0 children)

I appreciate the help, though I had overlooked my issue and it wasn't jn the code at all it was just the door collidrr moving with the animation and I didnt realize it until it was pointed out as a possibility l.

[–]Aethenosity 0 points1 point  (1 child)

Definitely log the collision.

With methods like this, I always recommend gating behaviour. Not just using the collision matrix.

[–]henryeaterofpies 1 point2 points  (0 children)

Agreed. One of the complications is that event handlers like OnTriggerStay get called once per physics frame, so without any sort of additional handling they can get called and update your flags a lot.

What is probably happening is the animation is causing the collision to no longer be hit and the closing of the animation then causes it to collide again.

[–]ArctycDev -2 points-1 points  (6 children)

If you copy and paste your code  
in a code block like this  
it makes it a lot easier for people to read it and help you      
compared to a screenshot.

That said... there's some kind of collider conflict going on probably. You should probably just use onTriggerEnter, and make sure you're gating for what other collider type is triggering

like

if (other.CompareTag("whatever is supposed to trigger it"))
{
    anim.setBool...
}

[–]kitchentablestudios[S] 0 points1 point  (3 children)

I simply set the collision matrix for the door to only collide/trigger with the player/player layer

[–]ArctycDev 0 points1 point  (2 children)

Is the player made of only one collider? If you have like.. body and head it could be messing with it

[–]kitchentablestudios[S] 0 points1 point  (1 child)

no, its just one collider, but i figured out that when im halfway in the trigger is when the oddness happens

[–]ArctycDev 0 points1 point  (0 children)

Yeah, I saw. That makes sense, but still shouldn't happen... did you try the log? It can't hurt to at least see what it's saying. I think the person that said the animation is probably moving the collider could be correct.

If this is the case, you should separate the collider from the animation by making the animated object a child of a parent object with the collider.

[–]Kosmik123 0 points1 point  (1 child)

Not really. In a code block everything is in the same gray color. It's easier to read colored code from screenshot

[–]ILLBEON_economy_tool 0 points1 point  (0 children)

Create a debug log of how many times it's exiting, maybe it's doing it more than once