Help with OnTriggerEnter Not working correctly?

I would like some helping on figuring out why this OnTriggerEnter doesn’t modify the booleans gotHit and canMove. This code is supposed to allow my player to be hit by the enemy and play a staggering animation while not allowing my player to move and reduce it’s health, but only the health part works.

void OnTriggerEnter(Collider hit)
     {
         if (hit.gameObject.name == "L Hand Box")
         {
             gotHit = true;
             health.CurrentVal -= 20;
             if (this.anim.GetCurrentAnimatorStateInfo(0).IsName("standing_react_large_gut"))
             {
                 canMove = false;
             }
             else
             {
                 canMove = true;
             }
         }
         else
         {
             gotHit = false;
         }
     }

You need to prefix your animator state name with the animation layer. Since it’s on the base layer, try using “Base.standing_react_large_gut”. If you’ve changed the layer name, use that name instead of Base.

I’m not sure if I get what you mean, so I tried this… and it didn’t work seeing as it is a string.

if (this.anim.GetCurrentAnimatorStateInfo(0).IsName("Base.standing_react_large_gut"))
            {
                canMove = false;
            }
            else
            {
                canMove = true;
            }

If this is not what you meant could you explain it in more detail please?

Yep, that’s exactly what I meant. It doesn’t matter that it’s a string, that’s just the format of Animator state names and it won’t work if it’s not in that format. Let’s check a few other things:

-Is the name of your animation layer in your Animator “Base”?

-Is the name of the animation state (not the animation clip) “standing_react_large_gut”? I believe it’s case-sensitive as well.

-Does the script return any errors in runtime? I assume not, but I just want to check.

-Obviously, your character needs to be in the standing_react_large_gut state at the time of collision for canMove to be cleared. Make sure it’s in the correct state.

I’d recommend putting some print statements in there with the canMove assignments to make sure it’s really not executing. Also, you can remove this from your if statement, I see no reason for it to be there.

-Is the name of your animation layer in your Animator “Base”? —YES

-Is the name of the animation state “standing_react_large_gut”? I believe it’s case-sensitive as well. —YES

-Does the script return any errors in runtime? I assume not, but I just want to check. —NO

-Obviously, your character needs to be in the standing_react_large_gut state for canMove to be cleared. — The thing is that it never even get’s to that point because gotHit never changes to True, and gotHit is the boolean that activates my “standing_react_large_gut” animation.
I have already checked with Debug.Log to see if my player is actually being hit by the enemy and it is, also the health works fine it’s just that boolean “gotHit = true” that doesn’t work properly.


Image of my Animator

If the health is being assigned, then so is gotHit. You can see in the inspector that it doesn’t change? Are you sure there aren’t any other scripts or any part of this script that quickly changes the value back?

Hey DiceMaster5, did you finally figure out what was causing the animator not to be triggered? I am facing the same issue as you are.