版权申明:
本文原创首发于以下网站:
博客园『优梦创客』的空间: https://www.cnblogs.com/raymondking123
优梦创客的官方博客: https://91make.top https://91make.top/
优梦创客的游戏讲堂: https://91make.ke.qq.com https://91make.ke.qq.com/
『优梦创客』的微信公众号: umaketop
目标:
给背景加上音效
给子弹加音效并按照按下时间来提高音阶
给背景加音效
首先我们先联系到课助老师免费获取音效, 然后导入到我们的项目中, 并拖给我们的 ENV 组件, 并把 Loop 选项打开即可.
给子弹加音效并按照按下时间来提高音阶
首先打开我们的 Projectile 把我们的 Biu 音效拖入, 然后打开我们的 Porjectile 脚本编写如下代码
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Tank : MonoBehaviour
- {
- public GameObject Bullet;// 子弹的预制体
- public Transform ShotPos;// 子弹发出的位置
- public bool gameover;
- public float lastFireTime = 0;
- public float pressTime = 0;
- // Start is called before the first frame update
- void Start()
- {
- }
- // Update is called once per frame
- void Update()
- {
- if (gameover)
- return;
- if (Input.GetButtonDown("Fire1"))
- {
- pressTime = Time.time;
- }
- if (Input.GetButton("Fire1")&&Time.time - lastFireTime>0.2f)
- {
- // 每一帧计算当前时间 - 按下时间之差 d
- float d = Time.time - pressTime;
- // 实例化一个子弹;
- var transProj = Instantiate(Bullet).transform;
- float f = d / 2;// 最大 2 秒内有变化, 超过 2 秒使用最高音高系数 (1)
- if (f> 1)
- {
- f = 1;
- }
- transProj.GetComponent<AudioSource>().pitch = 1 + f * 2;
- // 设置子弹的父节点;
- transProj.SetParent(transform, false);
- // 设置子弹的坐标;
- transProj.position = ShotPos.position;
- // 给子弹一个力, 可以让子弹往前移动;
- Vector3 v = ShotPos.forward * 20;
- transProj.GetComponent<Rigidbody>().AddForce(v, ForceMode.Impulse);
- lastFireTime = Time.time;
- }
- }
- }
这样进入游戏时持续按下射击键即可听到我们发出的子弹的音效的不同
来源: https://www.cnblogs.com/raymondking123/p/11495560.html