- Unity Certified Programmer:Exam Guide
- Philip Walker
- 396字
- 2021-06-18 18:30:12
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:
- In the Unity editor, navigate to the Project window and create a new folder inside the Resources folder. Name the new folder Sound.
- Drag and drop the Player_Bullet prefab from the Project panel into the Hierarchy panel.
- With Player_Bullet still selected, click on the Add Component button in the Inspector panel.
- In its dropdown, start typing (and select) Audio Source.
- 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:

- Play on Awake is automatically ticked. As you can imagine, as soon as Player_Bullet is instantiated, the sound will play.
- 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.
- 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.
- 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.
推薦閱讀
- Building Modern Web Applications Using Angular
- What's New in TensorFlow 2.0
- Boost C++ Application Development Cookbook(Second Edition)
- Manga Studio Ex 5 Cookbook
- The DevOps 2.4 Toolkit
- R的極客理想:工具篇
- 軟件工程
- 琢石成器:Windows環境下32位匯編語言程序設計
- SAP BusinessObjects Dashboards 4.1 Cookbook
- Appcelerator Titanium:Patterns and Best Practices
- Learning Unreal Engine Game Development
- 信息學奧林匹克競賽初賽精講精練
- Ionic Framework By Example
- 數據庫技術及應用(Access)(第2版)
- Hybrid Mobile Development with Ionic