添加地图
将地图拖进Prefabs里
将原来的地图删掉
把新的地图塞进去
调整地图参数
调整开始界面视角
添加音效
把这东西往上面拉就可以在unity里听声音
创建一个Audios文件夹来管理音频文件
给M16添加Audios Sourse组件
把音频变成3D效果
播放音效
在WeaponManager里
public class WeaponManager : NetworkBehaviour
{
...
//当前武器的音效
private AudioSource currentAudioSource;
...
public void EquipWeapon(PlayerWeapon weapon) {
...
//切换武器时要切换音效
currentAudioSource = weaponObject.GetComponent<AudioSource>();
if (IsLocalPlayer) {
//如果是本地玩家,用spatialBlend把音效变成2d;
currentAudioSource.spatialBlend = 0f;
}
}
//返回当前音效
public AudioSource GetCurrentAudioSource()
{
return currentAudioSource;
}
...
}
在PlayerShooting里
public class PlayerShooting : NetworkBehaviour
{
...
//编写每次射击前的逻辑
private void OnShoot()
{
...
//播放音频
weaponManager.GetCurrentAudioSource().Play();
}
...
}
给手枪添加冷却时间
在PlayerWeapon里
public class PlayerWeapon
{
...
public float shootCoolDownTime = 1.5f; //单发模式才有冷却cd,连发只受射速限制
}
在PlayerShooting里
public class PlayerShooting : NetworkBehaviour
{
...
private float shootCoolDownTime = 0f; //离上一次开枪的时间
void Update()
{
...
shootCoolDownTime += Time.deltaTime; //更新时间,用unity自带的api
//单发
if (currentWeapon.shootRate <= 0)
{
if (Input.GetButtonDown("Fire1") && shootCoolDownTime >= currentWeapon.shootCoolDownTime)
{
Shoot();
shootCoolDownTime = 0f;
}
}
...
}
...
}
给自动步枪添加后坐力
在PlayerWeapon里
public class PlayerWeapon
{
...
public float recoilForce = 1f; //后坐力,这里面貌似是给的速度
}
在PlayerController里
public class PlayerController : MonoBehaviour
{
...
private float recoilForce = 0f;//后坐力
//施加后坐力
public void AddrecoilForce(float newRecoilForce)
{
recoilForce += newRecoilForce;
}
...
private void PerformRotation()
{
//当小于某个数时,视为零
if (recoilForce < 0.1f)
recoilForce = 0f;
//水平方向的后坐力
if (yRotation != Vector3.zero || recoilForce > 0)
{
//rb.transform.up是Rotation的属性,body水平转动就是rotation的y轴变动
rb.transform.Rotate(yRotation + rb.transform.up * Random.Range(-1f * recoilForce, 1f * recoilForce));
}
if(xRotation != Vector3.zero || recoilForce > 0)
{
cameraRotationTotal += xRotation.x - recoilForce;//xRotion是每一帧相机向量,要加上.x
...
}
//削弱后坐力
recoilForce *= 0.4f;
}
}
在PlayerShooting里
public class PlayerShooting : NetworkBehaviour
{
...
//用于向playerController传参
private PlayerController playerController;
private int autoShootCount = 0; //记录射了多少发,前三发后坐力可以很小
void Start()
{
...
//获取引用
playerController = GetComponent<PlayerController>();
}
void Update()
{
...
else //连发
{
if (Input.GetButtonDown("Fire1"))
{
autoShootCount = 0;
...
}
...
}
}
//把后坐力的参数传进来
private void OnShoot(float recoilForce)
{
...
if (IsLocalPlayer) {
//添加后坐力
playerController.AddrecoilForce(recoilForce);
}
}
...
private void Shoot() {
autoShootCount++;
float recoilForce = currentWeapon.recoilForce;
//前三发后坐力小点
if(autoShootCount <= 3)
{
recoilForce *= 0.2f;
}
OnShootServerRpc(recoilForce);
...
}
}