官术网_书友最值得收藏!

Creating sounds for the player's bullets

Up until now, our game has been silent, but sound is an important factor in any game. In this section, we will be introducing our first sound component. We will make a start by creating sound effects for when our player fires a bullet.

Feel free to add your own type of bullet sound if you wish. You can add sound to your player's standard bullets as follows:

  1. In the Unity editor, navigate to the Project window and create a new folder inside the Resources folder. Name the new folder Sound.
  2. Drag and drop the Player_Bullet prefab from the Project panel into the Hierarchy panel.
  3. With Player_Bullet still selected, click on the Add Component button in the Inspector panel.
  4. In its dropdown, start typing (and select) Audio Source.
  5. Drag and drop the PlayerLaser.mp3 file into the AudioClip section of the Audio Source component. The following screenshot shows Player_Bullet selected. The audio file at the bottom left needs to be dragged into the Audio Source component at the right:
  1. Play on Awake is automatically ticked. As you can imagine, as soon as Player_Bullet is instantiated, the sound will play.
  2. If the volume is too high, simply lower it in the Audio Source component of the Inspector window.
As well as the Volume option in the Audio Source component, there is Pitch to change the sound of our bullet and Stereo Pan to make the sound more dominant on the left or right speaker. Finally, because this is a two-dimensional game, we don't want the sound to be affected by how close our camera is to the bullet. So, we slide the Spatial Blend toggle all the way to the left to make sure it is not affected by its distance.
  1. Finally, click on Apply at the top-right corner to save and update the Player_Bullet prefab and remove the bullet from the Hierarchy window.
  2. Play the scene and start firing. You will hear laser noises and in the scene view, you will see speaker symbols now attached to the player's bullets.

That brings us to the end of this short section on audio, but we will cover more on audio throughout this book. Don't forget that if you get stuck at any point, check the Complete folder for this chapter and compare the scenes and code to make sure nothing is missing.

主站蜘蛛池模板: 钟祥市| 榆树市| 峨山| 游戏| 周口市| 宜丰县| 客服| 清流县| 剑河县| 法库县| 弥渡县| 宿松县| 乡城县| 沙雅县| 法库县| 锡林浩特市| 兴宁市| 阳春市| 兴业县| 郯城县| 平阴县| 孟津县| 确山县| 确山县| 南昌市| 焦作市| 泰来县| 西乌珠穆沁旗| 彰武县| 桦川县| 安图县| 静宁县| 娄底市| 台州市| 曲阳县| 乃东县| 湘潭县| 富蕴县| 湖北省| 科技| 郴州市|