unity3d添加碰撞效果_Unity3D:在游戏中添加声⾳效果
unity3d添加碰撞效果
Audio is quite an interesting concept to cover, not only in game design but when studying its nature in general. The way it's perceived depends on a lot of factors, which have to be taken into account when you're trying to control or use it.
⾳频不仅在游戏设计中,⽽且在研究其本质时,都是⼀个⾮常有趣的概念。 感知⽅式取决于许多因素,在您尝试控制或使⽤它时必须考虑这些因素。
Perception of audio generally involves the position of the source and how fast it's moving if it's moving at all (If you've
Doppler Effect).
studied Physics, you may be familiar with the Doppler Effect
多普勒效应很熟悉)。
⾳频的感知通常涉及源的位置以及源的移动速度(如果已经移动的话)(如果您已经学习过物理,则可能对多普勒效应
positioning of a source of audio is important to characterize its source. For example, we need to make sure In Unity, the positioning of a source of audio
that a gameObject playing the sound of a waterfall matches with an actual waterfall gameObject, and that the player's
audio panning varies as the perception of that sound feels real. Getting louder as the player gets closer, making sure the audio panning
relative position of the waterfall changes with respect to the player, and so on.
定位对于表征其源很重要。 例如,我们需要确保播放瀑布声的gameObject与实际的瀑布gameObject匹配,并且玩在Unity中, ⾳频源
⾳频源的定位
⾳频平移随瀑布相对于播放器的相对位置的变化⽽变化,依此类推。
家对声⾳的感觉是真实的。 随着播放器靠近,声⾳会变⼤,请确保⾳频平移
We deal with 2 main components related to Audio in Unity, they are:
我们处理与Unity中的⾳频相关的2个主要组件,它们是:
1. Audio Listener
⾳频听众
2. Audio Source
⾳源
Let's have a look at these components.
让我们看⼀下这些组件。
⾳频听众 (Audio Listener)
This is a component that's automatically attached to the main camera every time you create a scene. It doesn't have any
point of perception. Leaving the Audio Listener as it is, is recommended. properties, since its only job is to act as the point of perception
感知点 。 建议保留⾳频侦听器原样。
每次创建场景时,此组件都会⾃动附加到主摄像机。 它没有任何属性,因为它的唯⼀⼯作就是充当感知点
⾳源 (Audio Source)
This is the component that's actually responsible for playing the sound. In common development practice, it's generally a empty gameObject to act as the Audio Source and make it a child
make it a child when you're dealing with large, good idea to make an empty gameObject
complex structures, so you have a clear idea of where the Audio Source is.
将其作
空的gameObject⽤作⾳频源,并在处理⼤型复杂结构时将其作这是实际上负责播放声⾳的组件。 在常见的开发实践中,通常最好将⼀个空的gameObject
为孩⼦ ,因此您对⾳频源的位置有清楚的了解。
为孩⼦
panning,
pitch, panning The Audio Source component has quite a few properties which we can tinker around with. This includes its pitch
Doppler spatial blending (We'll get to that later), and if you open the 3D Sound Settings, you will find options for adding Doppler spatial blending
volume rolloffs.
Effects and volume rolloffs
Effects
平移 , 空间混合
空间混合 (我们将在后⾯介绍),如果您打开3D声⾳设置 ,则会到
⾳调 , 平移
⾳频源组件具有许多我们可以修改的属性。 这包括其⾳调
⾳量衰减的选项 。
多普勒效果和⾳量衰减的选项
⽤于添加多普勒效果
AudioClip slot, however. That's where the sound effect to be played goes. Unity What interests us the most here is the AudioClip
supports quite a few common sound formats, including .mp3 and .ogg etc.
但是,最让我们感兴趣的是AudioClip
AudioClip插槽。 那就是要播放声⾳效果的地⽅。 Unity⽀持许多常见的声⾳格式,包括.mp3和.ogg等。
添加⾃⼰的声⾳效果 (Adding your Own Sound Effects)
Until sound-supportive documents are supported, we'll have to create our own sound effects. We have made a package of a
pair of small sound effects, which you can download from .
在⽀持声⾳的⽂档之前,我们必须创建⾃⼰的声⾳效果。 我们制作了⼀对⼩声⾳效果的程序包,您可以从下载。
Let's import these sound effects into our project by creating a new folder for sound effects and adding the sounds to it.
通过创建⼀个新的声⾳效果⽂件夹并将声⾳添加到其中,让我们将这些声⾳效果导⼊到我们的项⽬中。
Audio Sources to our relevant
Audio Listener, all we have to do is add Audio Sources
Now, since the main camera already has an Audio Listener
gameObjects.
⾳频源添加到相关的gameObjects中。
现在,由于主摄像机已经具有⾳频侦听器
⾳频侦听器 ,所以我们要做的就是将⾳频源
Unity 3D:添加⾳频源 (Unity 3D: Adding an Audio Source)
First off, we'll have a sound effect that plays when our character fires a fireball. For that, we'll simply attach an Audio
Add Component → Audio → Audio Source.
Source to our character's shooter gameObject. Go to Add Component → Audio → Audio Source
添加组件→⾳频→⾸先,当⾓⾊发射⽕球时,我们将播放声⾳效果。 为此,我们只需将⾳频源附加到⾓⾊的射击游戏gameObject。 转到添加组件→⾳频→
⾳频源 。
⾳频源
fire1.mp3. Now, In the AudioClip
AudioClip slot, drag in the sound effect you want to use. If you downloaded the soundpack, drag in fire1.mp3
Play on Awake checkbox. This makes sure that this sound doesn't play as soon as the gameObject awakes, untick the Play on Awake
which in our case is as soon as the game starts.
“唤醒时播放”复选框。
fire1.mp3 。 现在,取消选中“唤醒时播放”
AudioClip插槽中,拖⼊要使⽤的声⾳效果。 如果您下载了声⾳包,请拖⼊fire1.mp3
在AudioClip
这样可以确保在gameObject唤醒后就不会播放声⾳,在我们的情况下,这是在游戏开始时⽴即播放。
In the tutorial , we created a class Shooter which was instantiating a new fireball
unity 教程fireball every time Spacebar was hit, let's add sound when a new fireball is fired.
⽕球 ,让我们在发射⼀个新的⽕球时添加声⾳。
在“ ”教程中,我们创建了⼀个Shooter类,每次按下空格键时都会实例化⼀个新的⽕球
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shooter : MonoBehaviour
{
public GameObject fireball;
private AudioSource source;
void Start()
{
source = GetComponent<AudioSource>();
}
void Update()
{
// When spacebar is hit
if(Input.GetKeyDown(KeyCode.Space))
{
// instantiate the fireball object
Instantiate(fireball,
new ansform.position.x, ansform.position.y, 0),
new Quaternion(0, 0, 0, 0));
/
/ play the sound
source.Play();
}
}
}
AudioSource variable initially, the magic happens when we call, source.Play(). This line of Besides the declaration of the AudioSource
code makes the referenced AudioSource play the AudioClip that's loaded into its slot. It has an overloaded(variant) method as well for playing the sound effect after a certain specified delay.
AudioSource变量外,魔术还发⽣在我们调⽤source.Play() 。 这⾏代码使引⽤的AudioSource播放加载到其插槽中的
除了最初声明AudioSource
AudioClip。 它还具有重载(变体)⽅法,可以在特定的指定延迟后播放声⾳效果。
Now, save this script, and simply fill in the slot in the Player's AudioSource's AudioClip slot with your sound effect. (Use
fire1.mp3 if you're using our soundpack, that you just downloaded.)
现在,保存此脚本,并只需⽤您的声⾳效果填充播放器AudioSource的AudioClip插槽中的插槽即可。 (如果您使⽤的是刚刚下载的声⾳fire1.mp3 。)
包,请使⽤fire1.mp3
Similarly, for the target, we will update the script for class TargetBehaviour used in the tutorial and attach an Audio Source to it, then use the script:
同样,对于⽬标,我们将更新“ ”教程中使⽤的TargetBehaviour类的脚本,并将⾳频源附加到该脚本,然后使⽤该脚本:
public class TargetBehaviour : MonoBehaviour
{
private AudioSource source;
void Start()
{
source = GetComponent<AudioSource>();
}
void onCollisionEnter2D(Collision2D col)
{
// When target is hit
if(col.gameObject.tag == "Bullet")
{
Debug.Log("Target was Hit!");
/
/ calling AddScore method
ScoreBehaviour.AddScore();
// play sound
source.Play();
Destroy(col.gameObject);
Destroy(gameObject);
}
}
}
dead3.mp3 if you are using our
And fill in the AudioClip slot with your sound effect when the target gets destroyed (dead3.mp3
soundpack). Play the game, and you can hear the sound effects when the player shoots a fireball and when it hits a target!
(dead3.mp3如果您正在使⽤我们的soundpack)。 玩游戏,当玩家射击⽕球并击中⽽在⾳频剪辑插槽填写您的声⾳效果,当⽬标被摧毁(dead3.mp3
⽬标时,您会听到声⾳效果!
Unity 3D:添加声⾳效果摘要 (Unity 3D: Adding Sound Effects Summary)
All in all, you have to do the following things to add an audio clip to your game:
总⽽⾔之,您必须执⾏以下操作才能将⾳频剪辑添加到游戏中:
RigidBody or BoxCollider
BoxCollider component.
AudioSource component to your gameObject, like you add a RigidBody
1. Add an AudioSource
BoxCollider组件⼀样。
RigidBody或BoxCollider
将AudioSource
AudioSource组件添加到游戏对象,就像添加RigidBody
2. In the script attached to the gameObject, initialise a variable of type AudioSource
在游戏对象附带的脚本中,初始化AudioSource类型的变量
3. In the Start() method, use GetComponent to set the AudioSource component to the AudioSource variable.
在Start()⽅法中,使⽤GetComponent将AudioSource组件设置为AudioSource变量。
4. When you want the sound to play, call the Play() method.
当您想要播放声⾳时,请调⽤Play()⽅法。
unity3d添加碰撞效果
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论