日志2025.1.26

日志2025.1.26
1.增加了副武器系统,优化了切换武器的视觉效果
在人物背包、腿上挂载装有BackupWeaponModel脚本的武器模型
    //返回副武器
    public Weapon BackupWeapon()
    {
        foreach (var weapon in weaponSlots) { 
            //在武器槽中却不是当前武器,即为副武器
            if(weapon.weaponType != currentWeapon.weaponType)
            {
                return weapon;
            }
        }

        return null;
    }


    //显示副武器(挂在背上或腿上模型
    public void SwitchOnBackupWeaponModel()
    {
        WeaponType curType = player.weapon.BackupWeapon().weaponType;

        foreach (BackUpWeaponModel curBackupModel in backUpWeaponModels)
        {
            if(curBackupModel.weaponType == curType)
            {
                curBackupModel.gameObject.SetActive(true);
            }
        }

    }

    //把副武器(挂在背上或腿上)模型隐藏
    public void SwitchOffBackupWeaponModels()
    {
        foreach (BackUpWeaponModel cur in backUpWeaponModels)
        {
            cur.gameObject.SetActive(false);
        }
    }

    //显示玩家即将要装备在手上的枪的模型(用于动画事件)
    public void SwichOnCurrentWeaponModel()
    {
        //设置对应的动画层
        SwitchAnimationLayer((int)CurrentWeaponModel().holdType);

        SwitchOffBackupWeaponModels();

        //显示副武器模型
        if (!player.weapon.HasOnlyOneWeapon())
        {
            SwitchOnBackupWeaponModel();
        }

        //显示主武器模型
        SwitchOffWeaponModels();
        CurrentWeaponModel().gameObject.SetActive(true);
        AttachLeftHand();
    }


2.添加了武器装备速度和装弹速度的设置
在animator中增加reloadSpeed,equipsSpeed两个参数,然后在相应的动画层设置multiplier,并在脚本中控制


3.增加了子弹的对象池
public class ObjectPool : MonoBehaviour
{
    public static ObjectPool instance;

    [SerializeField] private GameObject bulletPrefab;
    [SerializeField] private int poolSize = 10;

    private Queue<GameObject> bulletPool;

    private void Awake()
    {
        //只能存在一个instance
        if(instance == null)
        {
            instance = this;
        }
        else
        {
            Destroy(gameObject);
        }

        bulletPool = new Queue<GameObject>();

    }

    private void Start()
    {
        CreatInitiaPool();
    }

    //从队列中获取子弹
    public GameObject GetBullet()
    {
        //如果当前子弹池没有子弹了,那么再创建新的子弹进入子弹池
        if(bulletPool.Count == 0)
        {
            CreateNewBullet();
        }

        GameObject bullet = bulletPool.Dequeue();
        bullet.SetActive(true);
        bullet.transform.parent = null;

        return bullet;
    }

    //将子弹返回队列中
    public void ReturnBullet(GameObject bullet)
    {
        bullet.SetActive(false);
        bullet.transform.parent = transform;

        bulletPool.Enqueue(bullet);
    }

    //创建最初的对象池
    private void CreatInitiaPool()
    {
        for (int i = 0; i < poolSize; i++)
        {
            CreateNewBullet();
        }
    }

    //创建子弹进入子弹池
    private void CreateNewBullet()
    {
        GameObject newBullet = Instantiate(bulletPrefab, transform);
        newBullet.SetActive(false); //不让子弹发生碰撞
        bulletPool.Enqueue(newBullet);
    }
}


4.增加了射速限制
    public float fireRate = 1;//一秒能射几发子弹
    private float lastFireTime;

    //能否射击
    public bool CanShoot()
    {
        if(HaveEnoughBullets() && ReadyToFire())
        {
            bulletsInMagzine--;
            return true;
        }

        return false;
    }

    //限制射速
    private bool ReadyToFire()
    {
        if(Time.time - lastFireTime > 1 / fireRate)
        {
            lastFireTime = Time.time;
            return true;
        }

        return false;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值