FPS游戏之子弹

最近在做策划给的一些关于FPS类型的需求,Demo基本已经成型,接下来整理并记录,高手略过,不喜勿喷!!!

上图

public float timeToLive = 2.0f;
private int damage = 2; 子弹的伤害

 void Update() {
        fwd = transform.TransformDirection(Vector3.forward);
        RaycastHit hit;

        if (Physics.Raycast(transform.position, fwd, out hit, BulletSpeed * 2.0f)) {
            if (!alreadyDestroyed && hit.collider.tag != "Bullets") {
                DestroyBullet(hit.normal, hit.point, hit.transform, hit.collider.gameObject, hit.collider.tag);
            }
        }

        timeToLive -= Time.deltaTime;
        if (timeToLive <= 0.0f) {
            //对象池回收
            FPS_ObjectPool.GetInstance().RecycleObj(gameObject);
           
        }
        
    }

 void DestroyBullet(Vector3 HitNormal, Vector3 HitPos, Transform HitTransform, GameObject Target, string HitTag) {

 //对象池回收
  FPS_ObjectPool.GetInstance().RecycleObj(gameObject);
 if (HitTag == "Ground") {
                GetFXFlash("BulletGenericDecal", HitNormal, HitPos);
}
    
 if (Target.GetComponent<FPS_Health>() != null) { 
             //伤害
             Target.GetComponent<FPS_Health>().ApplyDamage(damage);
            //敌人受伤特效
             GetFXFlash("VFX_BloodSplat", HitNormal, HitPos);
           
        }

}

  void GetFXFlash(string fxName,Vector3 HitNormal, Vector3 HitPos) {
        GameObject bullet = FPS_ObjectPool.GetInstance().GetObj(fxName);
        bullet.SetActive(false);
        bullet.transform.position = HitPos;
        bullet.transform.rotation = Quaternion.LookRotation(HitNormal);
        bullet.SetActive(true);
    }

猜你喜欢

转载自blog.csdn.net/u012909508/article/details/82907875