IEnumerator StartThrowingOrb(Vector2 atPosition) { // Play arm swing animation PlayOrbPlaceAnimation(atPosition); // Play orb throw sound throwOrbAudioSource.Play(); Orb newOrb = SpawnOrb(atPosition); // Delay based on distance float percentOfMaxRange = Mathf.Clamp01(Vector2.Distance(backpackTransform.position, atPosition) / ORB_THROW_DELAY_CAP_DISTANCE); float delayLength = Mathf.Lerp(0f, MAX_ORB_THROW_DELAY, percentOfMaxRange); OrbInactive inactiveOrb = Instantiate(orbInactivePrefab, backpackTransform.position, Quaternion.identity).GetComponent <OrbInactive>(); inactiveOrb.SetDestinationAndStartMoving(atPosition, delayLength); yield return(new WaitForSeconds(delayLength)); ActivateOrb(newOrb); }
IEnumerator StartRecallingOrb() { // Play recall sound recallOrbAudioSource.Play(); // Delay based on distance Orb orbToRecall = orbs.Dequeue(); Destroy(orbToRecall.gameObject); float percentOfMaxRange = Mathf.Clamp01(Vector2.Distance(backpackTransform.position, orbToRecall.transform.position) / ORB_THROW_DELAY_CAP_DISTANCE); float delayLength = Mathf.Lerp(0f, MAX_ORB_THROW_DELAY, percentOfMaxRange); OrbInactive inactiveOrb = Instantiate(orbInactivePrefab, orbToRecall.transform.position, Quaternion.identity).GetComponent <OrbInactive>(); inactiveOrb.SetDestinationAndStartMoving(backpackTransform, delayLength); // Start playing sound slightly before it arrives backpackAudioSource.PlayDelayed(delayLength * 0.75f); yield return(new WaitForSeconds(delayLength)); OnOrbReturned(); }