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;
pathfinder.SetSearch();
}
What happens in extended AIPath:
public void SetSearch() //this is called when stun ends or player aproaches
{
autoRepath.mode = AutoRepathPolicy.Mode.Dynamic;
SearchPath();
canMove = true;
Debug.Log("Search set.");
}
public void StopMovement() //this is called through OnBecameInvisible()
{
Debug.Log("Not moving");
ClearPath();
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.