Unity用面向对象的思想实现拾取随机武器的快捷方法

整体思路很简单,玩家捡道具(Trigger触发检测),碰到了,就切换武器

玩家有拿武器的位置,当前武器,切换武器的方法

道具类有此道具是什么武器的信息,还有调用玩家切换武器的方法

整体类图

玩家类 

 

public class PlayerObj : TankBaseObj
{
    //当前武器
    public WeaponObj nowWeapon;
    //武器父对象位置
    public Transform weaponPos;

    public void ChangeWeapon(GameObject obj)
    {
        //如果当前武器不为空
        if (nowWeapon != null)
        {
            //删除当前武器
            Destroy(nowWeapon.gameObject);
            nowWeapon = null;
        }

        //切换武器
        GameObject weaponObj = Instantiate(obj, weaponPos);
        //当前武器就是切换后的武器
        nowWeapon = weaponObj.GetComponent<WeaponObj>();
    }
}

道具类

public class WeaponReward : MonoBehaviour
{
    //自身是什么武器
    public GameObject weaponObj;

    //触发检测
    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            //调用玩家的切换武器方法
            PlayerObj player = other.GetComponent<PlayerObj>();
            player.ChangeWeapon(weaponObj);
            //销毁自身
            Destroy(this.gameObject);
        }
    }
}

 

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码

)">
< <上一篇
下一篇>>