Unity教程2D入门:18 音效Audio

https://www.bilibili.com/video/BV1R4411C7FD

在对预制体添加组件时,组件的左边框会有蓝色条,代表该组件只作用于该预制体,

如果想所有预制体都作用,点击下面Overrides》Apply All即可

最方便的更新方式是点击预制体右侧的箭头在里面的gameobject添加音效

 

 

Audio Listener声音接收器

Audio Source扬声器,音源

Audio Clips声音片段

Player添加Audio Source组件,音乐拖进Audio Clips里面

 这里先给enemy死亡一个声音

在Enemy脚本添加一个函数,并且在JumpOn()中调用(不需要拖拽调用)

public class Enemy : MonoBehaviour
{
    protected Animator anim;
    protected AudioSource deathAudio;//<===================================
    protected virtual void Start()
    {
        anim = GetComponent<Animator>();
        deathAudio = GetComponent<AudioSource>();
    }
    public void Death()
    {
        Destroy(gameObject);
    }
    public void JumpOn()
    {
        anim.SetTrigger("Death");
        deathAudio.Play();//<===================================
    }
}

 偷懒技巧:在Frog复制组件,在Eagle随便选一个组件粘贴,轻松复制死亡音效

 为Player添加跳跃音效,① 在player添加代码如下

    public AudioSource jumpAuido;//<==================================

    void Start()
    {
        jumpAuido = GetComponent<AudioSource>();//<==================================
    }
    void Movement()//移动代码
    {
        if (Input.GetKeyDown(KeyCode.U))
        {
            jumpAuido.Play();//<==================================
        }
    }

记得AudioSource要添加在预制体内的Player,否则会与BGM冲突

 后期添加吃樱桃,受伤害等方法和跳跃音效相同。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值