Add Folder to Google Paly Music and It Dies Not Upload

Playing a sound in Unity is relatively easy. Yous only need an Audio Source component to play the audio prune and an Sound Listener component to hear the audio. Essentially the Sound Source is a speaker playing the sound, and the Sound Listener is the ears of the game that hear the audio. Later giving a cursory rundown of each component, I will show how to use them to create sound effects for the Space Shooter from previous articles.

The Components

Here is a brief overview of each component before nosotros put them to piece of work in the game.

Audio Listener

The Audio Listener component is already applied to the Master Camera and is already on whatever Camera created by default. The Sound Listener component does not have any settings that you can alter. Typically, the Audio Listener is on the Main Camera, but it may be better to have it on the Player, depending on the game.

Unity tin only handle ane Audio Listener active in the scene at a time and will requite you a warning when there is more one active when in Play Way.

Audio Source

An Sound Source component has to exist added to a GameObject. The Audio Source component has many settings; near are pretty self-explanatory as to what they do. I volition list a few that nosotros will need for this article. For a complete description of each holding, see the Unity Documentation.

Sound Prune: Reference to the sound prune file that will be played.

Play On Awake: If enabled, the audio will start playing the moment the scene launches. If disabled, you need to start information technology using the Play() command from scripting.

Loop: Enable this to brand the Sound Clip loop when it reaches the end.

Priority: Determines the priority of this audio source among all the ones that coexist in the scene. (Priority: 0 = almost important. 256 = least important. Default = 128.). Use 0 for music tracks to avert it getting occasionally swapped out.

Volume: How loud the audio is at a distance of one world unit (1 meter) from the Audio Listener.

How to Utilize the Components

We will add groundwork music, a laser-firing audio, explosion sounds, and a power-up audio to prove how to use the components.

Background Music

The easiest thing to create is looping background music. All that is need is a GameObject with an Audio Source component ready to Loop and an Audio Prune that you lot want to exist played.

Create an Empty GameObject named "Sound Manager" to proceed the Bureaucracy organized for adding more sound effects later, goose egg out the position. Add an Sound Source component, enable Loop, and drag in the audio prune y'all want to play into the Sound Clip field. Now in Play Mode, the music_background audio clip volition first playing and loop when finished.

Annotation: If you want to create an Sound Source only for one Audio Clip that you have in the Assets binder, you can just drag that clip to the scene view — a GameObject with an Audio Source component volition be created automatically for it. Dragging a prune onto an existing GameObject will attach the clip along with a new Audio Source if at that place isn't one already there. If the object already has an Audio Source, then the newly dragged clip volition supercede the one that the source currently uses.

Laser Shot

In the Thespian script, add together a individual variable of blazon AudioClip named "_laserClip" with a zip value; this is a reference for the sound result that volition play. Then create a private variable of type AudioSource named "_source" with a null value; this is a reference to the Audio Source component on the Player that is told to play the laser sound.

In Start, retrieve the Audio Source component from the Histrion and assign it to _source, then null check _source and if it is not null change the clip of _source to _laserClip; this will set the audio prune in _laserClip to the Audio Source through code.

Add the Audio Source component to the Player GameObject, disable Play On Awake, and drag in the audio clip for the laser shot into the Player script Laser Prune field. At present the Actor will play the laser sound effect every time a Laser is fired.

If y'all enter Play Mode, you lot can see that the Laser Clip field's audio clip gets assigned to the AudioClip field of the Audio Source by the Histrion script. Now to go the audio issue to play when you burn a Laser.

Dorsum in the Histrion script, inside the FireLaser method, add together _source.Play(); this will tell the Audio Source to play the sound in its AudioClip variable. Now the laser shot sound will play every time the Histrion fires a Laser.

Before moving on to the Explosion sound furnishings, allow's make the Player'south Sound Source a piffling more than modular.

In the Player script, remove the else statement where the _source.prune is changed.

And then create a new public method named "PlayClip" and pass in a variable of blazon AudioClip called "clip"; this will allow you to play any sound you pass in. Inside the method, tell _source to play the sound passed-in by using PlayOneShot(clip). That will play the passed-in clip in one case using the Sound Source'due south settings, but unlike the Play function, the PlayOneShot part does non abolish clips that PlayOneShot or Play is already playing.

Finally, change the _source.Play() out with PlayClip(_laserClip); this will telephone call the PlayClip method and play _laserClip with the PlayOneTime function.

Explosions

At present to add together explosion sounds to the Asteroid and Enemy when destroyed.

First, let's add together the Asteroid explosion. Adding the explosion audio is very simple since we already instantiate an Explosion Prefab when the Asteroid is destroyed. Nosotros can simply add an Audio Source component to the Explosion Prefab, elevate in the audio clip for the explosion, and accept information technology Play On Awake. With that, the Asteroid at present has a sound event when destroyed. Now, time for the Enemy explosion.

Side by side in the Enemy script, add a private AudioClip named "_explosionClip" with a null value; to concur the explosion sound. Now, create a private AudioSource called "_source" with a nada value; to incorporate a reference to the Audio Source component on the Enemy Prefab. Then create a private bool named "_isDead" with a false value; we volition use it to ensure the Player can't trigger the explosion sound more than than in one case.

In Start, retrieve the Sound Source component from the Enemy and assign it to _source, so cipher cheque _source and if it is non null change the prune of _source to _explosionClip; this will alter the sound clip to the explosion sound result.

In OnTriggerEnter2D, inside the if statements checking the tag, add an if statement that checks if _isDead is false and if then, have _source play the sound prune and set _isDead to truthful, so it doesn't play again if collided with again.

Add together the Sound Source component to the Enemy Prefab, disable Play On Awake, and drag the sound clip for the explosion into the Enemy script's Explosion Clip field. Now the Enemy volition play an explosion sound effect in one case it dies from either Laser or Role player collision.

PowerUp

The PowerUp creates an interesting problem since it is destroyed on contact with the Player and does not instantiate annihilation. There are multiple ways to handle this; I will show two.

In the PowerUp script, add a individual AudioClip named "_powerUpClip" with a null value; there is just an audio prune considering the PowerUp gets destroyed right away, so at that place is no reason to have an Sound Source.

The first way of treatment this problem is to create a new GameObject with an Audio Source on it to play the clip and then destroy itself when the clip is complete. Luckily Unity has a function for this already in the AudioSource grade called PlayClipAtPoint, which takes in an audio clip to play and a Vector3 for a position for it to create the GameObject. After the clip has finished, it destroys itself.

In the OnTriggerEnter2D, before the PowerUp is destroyed add together AudioScource.PlayClipAtPoint( _powerUpClip, transform.position); this volition phone call the PlayClipAtPoint part in the AudioSource grade and pass information technology the _powerUpClip to play at the PowerUp's position.

Select all the PowerUp Prefabs at one time, then drag in the audio prune for the ability-up audio into the PowerUp script Ability Upward Clip field, and this will use to all of them at in one case since they all accept the aforementioned script and variable field.

Now it is working like a charm. The only problem with the GameObject creation method is that it can clutter the Hierarchy if a lot of them get called in a short time.

The second way of treatment the problem is to use the Player script's PlayClip method. Since nosotros are already getting a reference to Thespian for the PowerUp activation, it volition be easy to use PlayClip and pass in the _powerUpClip for it to play.

In the OnTriggerEnter2D, inside the histrion null check, telephone call the PlayClip method on the histrion and pass in _powerUpClip. That will send the _powerUpClip to exist played by the PlayClip method using PlayOneShot on the Player's Audio Source. Every bit mentioned in a higher place, the PlayOneShot function does non cancel clips that are already being played, so the power-up audio will not interrupt the laser sound or vice versa.

With that, the game now has sound and feels and then much more than interactive because of information technology. In the following commodity, we will look into building the game into a stand-lone awarding.

delimadishoursenot.blogspot.com

Source: https://medium.com/nerd-for-tech/playing-sound-effects-in-unity-a0e4987a4b45

0 Response to "Add Folder to Google Paly Music and It Dies Not Upload"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel