其他分享
首页 > 其他分享> > FPSMicroGame 武器系统

FPSMicroGame 武器系统

作者:互联网

FPSMicroGame 武器系统

射击

散射的计算WeaponController.cs脚本里。在GetShotDirectionWithinSpread函数里,主要是通过Vector3.Slerp,将 枪口的前向向量 和 一个随机的从球心到球面的单位向量,做插值。插值比例就是BulletSpreadAngle / 180f。可以想象一下, 两个三维向量角度之差最多是180°,所以BulletSpreadAngle 是最大散射角。

 void HandleShoot()
 {      
     //...
     // spawn all bullets with random direction
     for (int i = 0; i < bulletsPerShotFinal; i++)
     {
         Vector3 shotDirection = GetShotDirectionWithinSpread(WeaponMuzzle);
         ProjectileBase newProjectile = Instantiate(ProjectilePrefab, WeaponMuzzle.position,
                                                    Quaternion.LookRotation(shotDirection));
         newProjectile.Shoot(this);
     }
     //...
 }
 
 
 public Vector3 GetShotDirectionWithinSpread(Transform shootTransform)
 {
     float spreadAngleRatio = BulletSpreadAngle / 180f;
     Vector3 spreadWorldDirection = Vector3.Slerp(shootTransform.forward, UnityEngine.Random.insideUnitSphere,
                                                  spreadAngleRatio);
 
     return spreadWorldDirection;
 }

在这里插入图片描述
1. 枪口开火特效:在射击时,在枪口位置,实例化了一个粒子特效 prefabMuzzleFlashPrefab。并且在2s后销毁。
2. 子弹弹道,也是实例化。

void HandleShoot()
{
    //生成子弹弹道 spawn all bullets with random direction
    for (int i = 0; i < bulletsPerShotFinal; i++)
    {
        Vector3 shotDirection = GetShotDirectionWithinSpread(WeaponMuzzle);
        ProjectileBase newProjectile = Instantiate(ProjectilePrefab, WeaponMuzzle.position,
                                                   Quaternion.LookRotation(shotDirection));
        newProjectile.Shoot(this);
    }
    
    //枪口特效 muzzle flash
    if (MuzzleFlashPrefab != null)
    {
        GameObject muzzleFlashInstance = Instantiate(MuzzleFlashPrefab, WeaponMuzzle.position,
                                                     WeaponMuzzle.rotation, WeaponMuzzle.transform);
        // Unparent the muzzleFlashInstance
        if (UnparentMuzzleFlash)
        {
            muzzleFlashInstance.transform.SetParent(null);
        }

        Destroy(muzzleFlashInstance, 2f);
    }
    
}
  3. 过热导致的红色烟雾:这个在另一个脚本OverheatBehavior.cs里面。这里可以看到烟雾的控制是通过调整粒子系统的rateOverTimeMultiplier参数。它会调整粒子曲线的幅度。同时枪械在过热时的颜色也会发生改变。
   void Update()
   {
       // visual smoke shooting out of the gun 枪械在过热时的颜色。
       float currentAmmoRatio = m_Weapon.CurrentAmmoRatio;
       if (currentAmmoRatio != m_LastAmmoRatio)
       {
           m_OverheatMaterialPropertyBlock.SetColor("_EmissionColor",
                                                    OverheatGradient.Evaluate(1f - currentAmmoRatio));
   
           foreach (var data in m_OverheatingRenderersData)
           {
               data.Renderer.SetPropertyBlock(m_OverheatMaterialPropertyBlock, data.MaterialIndex);
           }
   
           //过热烟雾
           m_SteamVfxEmissionModule.rateOverTimeMultiplier = SteamVfxEmissionRateMax * (1f - currentAmmoRatio);
   }
- 音效

    - 开火音效分为两种,连续开火音效 和 单发开火音效。具体看WeaponController文件,这个根据UseContinuousShootSound变量来区分。一个AudioSource可以通过 PlayOneShot 函数,播放多个声音片段。
 	//连续射击音效    
	void UpdateContinuousShootSound()
       {
           if (UseContinuousShootSound)
           {
               if (m_WantsToShoot && m_CurrentAmmo >= 1f)
               {
                   if (!m_ContinuousShootAudioSource.isPlaying)
                   {
                       m_ShootAudioSource.PlayOneShot(ShootSfx);
                       m_ShootAudioSource.PlayOneShot(ContinuousShootStartSfx);
                       m_ContinuousShootAudioSource.Play();
                   }
               }
               else if (m_ContinuousShootAudioSource.isPlaying)
               {
                   m_ShootAudioSource.PlayOneShot(ContinuousShootEndSfx);
                   m_ContinuousShootAudioSource.Stop();
               }
           }
       }
   
   //单发开火音效
       void HandleShoot()
       {
           // play shoot SFX
           if (ShootSfx && !UseContinuousShootSound)
           {
               m_ShootAudioSource.PlayOneShot(ShootSfx);
           }
       }

标签:FPSMicroGame,void,Vector3,系统,音效,后坐力,Time,武器,position
来源: https://blog.csdn.net/nikoong/article/details/116592111