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

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.

主站蜘蛛池模板: 福海县| 通榆县| 富民县| 喀什市| 广西| 湖北省| 马边| 诏安县| 桂阳县| 五家渠市| 招远市| 宣威市| 兴文县| 精河县| 建宁县| 鲁甸县| 原平市| 连山| 尚义县| 白朗县| 宁化县| 富阳市| 建瓯市| 耒阳市| 衡山县| 杂多县| 靖宇县| 咸阳市| 手游| 龙门县| 竹北市| 宝鸡市| 宝清县| 武穴市| 黑龙江省| 富宁县| 许昌市| 平邑县| 青龙| 府谷县| 玉山县|