Has the project something to do with OnBecameInvisible()?

I use OnBecameInvisible() to disable an enemy’s pathfinding and encountered a bug trying to kick him around by manually applying a force to its rigidbody.

What happens on stun effect:

private IEnumerator StunTimer(Vector2 direction)
        rigidBody2D.AddForce(direction * knockbackForce, ForceMode2D.Impulse);

        Debug.Log("Stun started");

        yield return new WaitForSeconds(stunTime);

        Debug.Log("Stun ended");
        //Debug.Log("Distance tavelled is " + Vector2.Distance(startPos, transform.position));

        rigidBody2D.velocity = Vector2.zero;

What happens in extended AIPath:

public void SetSearch() //this is called when stun ends or player aproaches
        autoRepath.mode = AutoRepathPolicy.Mode.Dynamic;
        canMove = true;
        Debug.Log("Search set.");

    public void StopMovement() //this is called through OnBecameInvisible()
        Debug.Log("Not moving");
        autoRepath.mode = AutoRepathPolicy.Mode.Never;
        canMove = false;

OnBecameInvisible() gets called when expected, but also randomly around the time of calling StunTimer(), sometimes before and sometimes after it ends. It does’t seem to depend much on direction, collision or distance travelled, but mostly happens when launched upwards. It is called after no longer that 0.91 seconds, and usually at about 0.5 - 0.6, but I managed to call it on 0.36 after starting the coroutine.


No, this package never calls OnBecameInvisible. So I think this issue is unrelated to this package.

Thanks, one less possible source.

P.S. Ok, checked it one more time, it seems to be not related to the stun coroutine at all. I’m sorry for bothering you.

1 Like