Skip to content

Commit

Permalink
Sounds effects for pickups (#37)
Browse files Browse the repository at this point in the history
* Use OneShot, multiple audio clips

* Pickup prefabs specify sound effects to play when picked up
  • Loading branch information
benvinegar authored Sep 25, 2024
1 parent 9499d00 commit db7df3b
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 8 deletions.
3 changes: 2 additions & 1 deletion Assets/Prefabs/Pickups/HotdogPickup.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: c95e69fb4311c48dbb4e870057afff87, type: 3}
m_Name:
m_EditorClassIdentifier:
_pickupSound: {fileID: 8300000, guid: 9f38fc745c8b64043b4a6a8d9ab8deab, type: 3}
_scoreValue: 50
_effectDuration: 0
_pickupType: 0
_effectText:
_healAmount: 30
--- !u!212 &2235537914035941605
SpriteRenderer:
Expand Down
1 change: 1 addition & 0 deletions Assets/Prefabs/Pickups/MoneyPickup.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 3fac4098d86d04d2184062b7557edeff, type: 3}
m_Name:
m_EditorClassIdentifier:
_pickupSound: {fileID: 8300000, guid: 9f38fc745c8b64043b4a6a8d9ab8deab, type: 3}
_scoreValue: 5000
_effectDuration: 0
_effectText: +5000 points!
Expand Down
3 changes: 2 additions & 1 deletion Assets/Prefabs/Pickups/MozartPickup.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: d09e6ada1b1694a018fc52fac6c953da, type: 3}
m_Name:
m_EditorClassIdentifier:
_pickupSound: {fileID: 8300000, guid: 4a45c3d67edb843519890127da0f36f3, type: 3}
_scoreValue: 25
_effectDuration: 0
_pickupType: 4
_effectText:
--- !u!212 &2235537914035941605
SpriteRenderer:
m_ObjectHideFlags: 0
Expand Down
1 change: 1 addition & 0 deletions Assets/Prefabs/Pickups/SkateboardPickup.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: caa06202f60e64f4f941f50f13d10dd8, type: 3}
m_Name:
m_EditorClassIdentifier:
_pickupSound: {fileID: 8300000, guid: 4a45c3d67edb843519890127da0f36f3, type: 3}
_scoreValue: 50
_effectDuration: 10
_effectText:
Expand Down
3 changes: 2 additions & 1 deletion Assets/Prefabs/Pickups/UmbrellaPickup.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: f1a09b23dd4b74942ae1cb83b2a925d2, type: 3}
m_Name:
m_EditorClassIdentifier:
_pickupSound: {fileID: 8300000, guid: 4a45c3d67edb843519890127da0f36f3, type: 3}
_scoreValue: 150
_effectDuration: 10
_pickupType: 2
_effectText:
_damageReduction: 0.5
--- !u!212 &2235537914035941605
SpriteRenderer:
Expand Down
4 changes: 3 additions & 1 deletion Assets/Scenes/BattleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -30320,6 +30320,8 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 8a84c6500790949f9ac1a9421f7c7876, type: 3}
m_Name:
m_EditorClassIdentifier:
_enemyHitSound: {fileID: 8300000, guid: f7b9b52a0dd6e49ebb93103f2f4c8e9f, type: 3}
_pickupSound: {fileID: 8300000, guid: 9f38fc745c8b64043b4a6a8d9ab8deab, type: 3}
_hitSoundCooldown: 0.1
--- !u!4 &1686016597
Transform:
Expand All @@ -30346,7 +30348,7 @@ AudioSource:
m_Enabled: 1
serializedVersion: 4
OutputAudioMixerGroup: {fileID: 0}
m_audioClip: {fileID: 8300000, guid: f7b9b52a0dd6e49ebb93103f2f4c8e9f, type: 3}
m_audioClip: {fileID: 0}
m_PlayOnAwake: 0
m_Volume: 0.4
m_Pitch: 1
Expand Down
19 changes: 16 additions & 3 deletions Assets/Scripts/Managers/SoundManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

public class SoundManager : MonoBehaviour
{
private AudioSource _enemyHitSound;
private AudioSource _audioSource;

[SerializeField]
private AudioClip _enemyHitSound;

private float _timeOfLastHitSound = 0f;

[SerializeField]
Expand All @@ -30,7 +34,12 @@ void Awake()

public void Init()
{
_enemyHitSound = GetComponent<AudioSource>();
_audioSource = GetComponent<AudioSource>();
}

public void PlayPickupSound(AudioClip clip)
{
_audioSource.PlayOneShot(clip);
}

public void PlayHitSound()
Expand All @@ -41,7 +50,11 @@ public void PlayHitSound()
return;
}

_enemyHitSound.Play();
// NOTE: don't use PlayOneShot on hit sounds because so many hits can
// happen that it can cause the audio source to die (no more sound for
// remainder of game session). Better to play manually.
_audioSource.clip = _enemyHitSound;
_audioSource.Play();
_timeOfLastHitSound = Time.time;
}
}
6 changes: 6 additions & 0 deletions Assets/Scripts/Pickups/PickupBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

public class PickupBase : MonoBehaviour
{
[SerializeField]
protected AudioClip _pickupSound;

[SerializeField]
[Tooltip("How much score this pickup is worth")]
protected int _scoreValue = 50;
Expand All @@ -28,6 +31,9 @@ private void OnTriggerEnter2D(Collider2D other)

OnCollect(player);

if (_pickupSound != null)
SoundManager.Instance.PlayPickupSound(_pickupSound);

Player.Instance.SpawnPlayerText(GetEffectText());

// Destroy the pickup
Expand Down
2 changes: 1 addition & 1 deletion Assets/Sounds/damageTaken.mp3.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Assets/Sounds/pickup.wav
Binary file not shown.
23 changes: 23 additions & 0 deletions Assets/Sounds/pickup.wav.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Assets/Sounds/powerUp.wav
Binary file not shown.
23 changes: 23 additions & 0 deletions Assets/Sounds/powerUp.wav.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit db7df3b

Please sign in to comment.