Course Content
Fighting Game in Unity
Fighting Game in Unity
Sounds for the Game
Audio Listener
The Audio Listener
is a component in Unity that acts as the ears of the game. Typically attached to the main camera or player’s character, it captures all audio in the scene. There should only be one Audio Listener
per scene.
Function: It listens to all audio sources and determines what the player hears, capturing 3D spatial audio, allowing the player to perceive the direction and distance of sounds.
Background Music with Audio Source
The Audio Source
component is used to play audio clips in Unity. It can be attached to any GameObject
, such as a dedicated object for background music or objects in the scene emitting sound effects.
Function: The Audio Source
plays audio clips and can be controlled and configured using various parameters.
Main Parameters in the Audio Source Component
AudioClip: The audio file to be played by this Audio Source
.
Output: The Audio Mixer Group
that this audio source will route its audio through. If none is specified, it uses the default audio output.
Mute: When checked, this will mute the audio source, preventing it from playing any sound.
Bypass Effects: Bypasses any effects applied to this audio source, meaning it will play without any applied audio effects.
Bypass Listener Effects: Ignores effects that are applied to the Audio Listener
.
Bypass Reverb Zones: Prevents the audio source from being affected by reverb zones in the scene.
Play On Awake: When checked, the audio source will start playing the assigned audio clip as soon as the scene starts.
Loop: When checked, the audio source will loop the audio clip, continuously replaying it until stopped.
Priority: Determines the priority of the audio source. Lower values mean higher priority, which is useful when many audio sources are playing simultaneously.
Volume: Controls the volume of the audio source. Example: 0.263 means the audio will play at about 26% of its original volume.
Pitch: Adjusts the pitch of the audio clip. The default value is 1. Lower values will slow down and deepen the sound, while higher values will speed up and raise the pitch.
Stereo Pan: Controls the stereo panning of the audio. A value of -1 pans the audio fully to the left, 1 pans fully to the right, and 0 centers the audio.
Spatial Blend: Determines the mix between 2D and 3D audio. A value of 0 makes the sound fully 2D (not affected by the listener's position), while 1 makes it fully 3D (affected by the listener's position).
Reverb Zone Mix: Adjusts the amount of the audio source that is mixed into the reverb zones in the scene. The default value is 1.
Summary
Audio Listener: Captures all audio in the scene and is typically attached to the main camera;
Audio Source: Plays audio clips and can be configured with various parameters to control how the audio is played and heard;
Main Parameters: Include AudioClip, Output, Mute, Bypass Effects, Play On Awake, Loop, Priority, Volume, Pitch, Stereo Pan, Spatial Blend, and Reverb Zone Mix.
These components work together to manage and control the audio experience in Unity, providing immersive and dynamic sound environments for games.
Playing the Attack Sound
Explanation of How the AudioSource is Used
Raycast Detection: A raycast is used to detect if the player’s attack hits an enemy. The raycast is cast from the player's position in the direction they are facing, with a range defined by attackRange
.
Checking for Hit: If the raycast hits an object within the enemyLayer
, the script checks if the hit object implements the IGetAttacked
interface.
Playing the Attack Sound: When a hit is detected and the enemy can be attacked (IGetAttacked
interface is implemented), the attack sound is played: attackAudioSource.Play()
.
Summary
In the PlayerAttack
class, AudioSource
plays a sound effect when the player successfully attacks an enemy, triggered by attackAudioSource.Play()
in the Update
method after a raycast hit. This provides immediate auditory feedback, enhancing the game's immersion.
Everything was clear?