Blawnode
Blawnode

Reputation: 13

Unity - The Animator won't let not me and not the code to disable a collider. Why?

I have a character game object, with both an animator and a collider.

Whenever the animator is on, the collider cannot be changed during run-time, though it can be changed in scene editing mode, via the inspector.

No matter what animator properties I change via the inspector, nothing happens. The feature I've tried to fix was invincibility frames - A co-routine, disabling the character's hit-box for several seconds.

I attempted enabling and disabling the collider's isTrigger property, but the problem persisted - The character still gets hurt while the isTrigger is on.

Code:

private IEnumerator ActivateInvincibility()
    {
        // 3 seconds of invincibility
        _hit_zone.enabled = false;  // no effect
        _hit_zone.isTrigger = true;  // no effect either
        yield return new WaitForSeconds(3f);
        _hit_zone.enabled = true;
        _hit_zone.isTrigger = false;;
    }

Called normally, like so: StartCoroutine("ActivateInvincibility");.

Edit:

For clarification, what I want to know is the root of the problem I've encountered, because I suspect more problems could occur because of this one root.

I have also edited out the unimportant lines of code.

Upvotes: 1

Views: 228

Answers (1)

alex
alex

Reputation: 11

//Added control flag
private bool isInvincibility = false;

private IEnumerator ActivateInvincibility()
{
    isInvincibility = true;
    //wait time
    isInvincibility = false;
}

void OnCollisionEnter(Collision collision)//or trigger {
     if(!isInvincibility && */Your condition tag name etc/*){
        //Damage ?
     }
}

psdt; call coroutine as StartCoroutine(ActivateInvincibility());. Don't use string name.

Upvotes: 0

Related Questions