IEnumerator LimitFreeFlyDistance(Rigidbody rb, float maxLength)
{
StopCoroutine("LimitFreeFlyDistance"); // In case of one is running.
// Give one physics update frame for the eject force to apply.
yield return new WaitForFixedUpdate();
// Figure out how much time it will take for harpoon to fly at the maximum distance.
var maxTimeToFly = cableJointLength / rb.velocity.magnitude;
KAS_Shared.DebugLog(
"Projectile {0} has been ejected at speed {1}. Max cable length {2} will be exahusted"
+ " in {3} seconds.",
rb, rb.velocity.magnitude, maxLenght, maxTimeToFly);
yield return new WaitForSeconds(maxTimeToFly + 0.5f); // Add a delta just in case.
// Restore performance mode if harpoon hasn't hit anyting.
if (rb.collisionDetectionMode != CollisionDetectionMode.Discrete) {
rb.collisionDetectionMode = CollisionDetectionMode.Discrete;
KAS_Shared.DebugLog(
"Projectile {0} hasn't hit anything. Reset collision check mode to Discrete", rb);
}
}