private int CalculateNextClipToPlay(ItemSelectionMethod selectionMethod) { int clipToPlay = 0; if (selectionMethod == ItemSelectionMethod.Randomly || m_Sounds.Length == 1) { clipToPlay = Random.Range(0, m_Sounds.Length); } else if (selectionMethod == ItemSelectionMethod.RandomlyButExcludeLast) { // Place the last played sound first in the array. AudioClip firstClip = m_Sounds[0]; m_Sounds[0] = m_Sounds[m_LastSoundPlayed]; m_Sounds[m_LastSoundPlayed] = firstClip; // Then play a random sound but exclude the first one. clipToPlay = Random.Range(1, m_Sounds.Length); } else if (selectionMethod == ItemSelectionMethod.InSequence) { clipToPlay = (int)Mathf.Repeat(m_LastSoundPlayed + 1, m_Sounds.Length); } return(clipToPlay); }
/// <summary> /// Will play a sound from the array of sounds, based on the chosen selection method. /// NOTE: Will use the AudioSource.PlayClipAtPoint() method, which doesn't include pitch variation. /// </summary> /// <param name="selectionMethod">How should the audio clip be selected?... at random? consecutively? (see ClipSelectionMethod description for more info)</param> public void PlaySound(ItemSelectionMethod selectionMethod, SoundType soundType, float volumeFactor = 1f, Vector3 position = default(Vector3)) { if (soundType == SoundType.BulletImpact) { m_BulletImpactSounds.PlayAtPosition(selectionMethod, position, volumeFactor); } else if (soundType == SoundType.Footstep) { m_FootstepSounds.PlayAtPosition(selectionMethod, position, volumeFactor); } else if (soundType == SoundType.Jump) { m_JumpSounds.PlayAtPosition(selectionMethod, position, volumeFactor); } else if (soundType == SoundType.Land) { m_LandSounds.PlayAtPosition(selectionMethod, position, volumeFactor); } else if (soundType == SoundType.Chop) { m_ChopSounds.PlayAtPosition(selectionMethod, position, volumeFactor); } else if (soundType == SoundType.Hit) { m_HitSounds.PlayAtPosition(selectionMethod, position, volumeFactor); } else if (soundType == SoundType.SpearPenetration) { m_SpearPenetrationSounds.PlayAtPosition(selectionMethod, position, volumeFactor); } else if (soundType == SoundType.ArrowPenetration) { m_ArrowPenetrationSounds.PlayAtPosition(selectionMethod, position, volumeFactor); } }
/// <summary> /// Will play a sound from the array of sounds, based on the chosen selection method. /// </summary> /// <param name="selectionMethod">How should the audio clip be selected?... at random? consecutively? (see ClipSelectionMethod description for more info)</param> public void PlaySound(ItemSelectionMethod selectionMethod, SoundType soundType, float volumeFactor = 1f, AudioSource audioSource = null) { if (soundType == SoundType.BulletImpact) { m_BulletImpactSounds.Play(selectionMethod, audioSource, volumeFactor); } else if (soundType == SoundType.Footstep) { m_FootstepSounds.Play(selectionMethod, audioSource, volumeFactor); } else if (soundType == SoundType.Jump) { m_JumpSounds.Play(selectionMethod, audioSource, volumeFactor); } else if (soundType == SoundType.Land) { m_LandSounds.Play(selectionMethod, audioSource, volumeFactor); } else if (soundType == SoundType.Chop) { m_ChopSounds.Play(selectionMethod, audioSource, volumeFactor); } else if (soundType == SoundType.Hit) { m_HitSounds.Play(selectionMethod, audioSource, volumeFactor); } else if (soundType == SoundType.SpearPenetration) { m_SpearPenetrationSounds.Play(selectionMethod, audioSource, volumeFactor); } else if (soundType == SoundType.ArrowPenetration) { m_ArrowPenetrationSounds.Play(selectionMethod, audioSource, volumeFactor); } }
public void Play2D(ItemSelectionMethod selectionMethod = ItemSelectionMethod.RandomlyButExcludeLast) { if (m_Sounds.Length == 0) { return; } int clipToPlay = CalculateNextClipToPlay(selectionMethod); GameController.Audio.Play2D(m_Sounds[clipToPlay], Random.Range(m_VolumeRange.x, m_VolumeRange.y)); }
/// <summary> /// Will use the AudioSource.PlayClipAtPoint() method, which doesn't include pitch variation. /// </summary> public void PlayAtPosition(ItemSelectionMethod selectionMethod, Vector3 position, float volumeFactor = 1f) { if (m_Sounds.Length == 0) { return; } int clipToPlay = CalculateNextClipToPlay(selectionMethod); AudioSource.PlayClipAtPoint(m_Sounds[clipToPlay], position, Random.Range(m_VolumeRange.x, m_VolumeRange.y) * volumeFactor); m_LastSoundPlayed = clipToPlay; }
/// <summary> /// /// </summary> public void Play(ItemSelectionMethod selectionMethod, AudioSource audioSource, float volumeFactor = 1f) { if (!audioSource || m_Sounds.Length == 0) { return; } int clipToPlay = CalculateNextClipToPlay(selectionMethod); var volume = Random.Range(m_VolumeRange.x, m_VolumeRange.y) * volumeFactor; audioSource.pitch = Random.Range(m_PitchRange.x, m_PitchRange.y); audioSource.PlayOneShot(m_Sounds[clipToPlay], volume); m_LastSoundPlayed = clipToPlay; }