在游戏对象中搜索标签,然后播放声音

本文关键字:然后 播放声音 标签 搜索 游戏 对象 | 更新日期: 2023-09-27 18:17:56

我有两个不同的空gameObjects, currentweapon被实例化到,其中一个依赖于枪,因为它们需要在不同的位置。我想在两个空的gameObjects中搜索特定的tags,如果它们中的objecttag,则播放特定的枪声。然而,有两个问题,awp枪没有被实例化到正确的空gameObject中,而且枪的声音没有播放。

WeaponManager脚本,其中枪被分配给一个枪持有人(空gameObject):

[SerializeField]
    private Transform tecweaponHolder;
    [SerializeField]
    private Transform awpweaponHolder;
    [SerializeField]
    private PlayerWeapon primaryWeapon;
    [SerializeField]
    private PlayerWeapon secondaryWeapon;
    private PlayerWeapon currentWeapon;
    private WeaponGraphics currentGraphics;
    private Transform currentHolder;
    void Start ()
    {
        currentHolder = awpweaponHolder;
        EquipWeapon(primaryWeapon);
    }
    void Update()
    {
        if(Input.GetAxis("WeaponSwitch") >0f)
        {
            currentHolder = tecweaponHolder;
            EquipWeapon(secondaryWeapon);
            Debug.Log("switched weapon");
        }
        else if (Input.GetAxis("WeaponSwitch")<0f)
        {
            currentHolder = awpweaponHolder;
            EquipWeapon(primaryWeapon);
            Debug.Log("switched weapon back");
        }
    }
    public PlayerWeapon GetCurrentWeapon ()
    {
        return currentWeapon;
    }

PlayerShoot脚本,搜索标签并播放适当的枪声:

    [SerializeField]
    private Transform tecweaponHolder;
    [SerializeField]
    private Transform awpweaponHolder;

    void Start ()
    {
        if (cam == null)
        {
            Debug.LogError("PlayerShoot: No camera referenced!");
            this.enabled = false;
        }
        weaponManager = GetComponent<WeaponManager>();
    }
    void Update ()
    {
        currentWeapon = weaponManager.GetCurrentWeapon();
        if (PauseMenu.IsOn)
            return;
        if (currentWeapon.fireRate <= 0f)
        {
            if (Input.GetButtonDown("Fire1"))
            {
                Shoot();
                foreach (Transform child in tecweaponHolder)
                {
                    if(gameObject.tag == "Tec")
                    {
                        CmdPlayTecShootSound();
                        Debug.Log("tec sound played");
                    }
                }
                foreach (Transform child in awpweaponHolder)
                {
                    if(gameObject.tag == "Awp")
                    {
                        CmdPlayAwpShootSound();
                        Debug.Log("awp sound played");
                    }
                }

在游戏对象中搜索标签,然后播放声音

Replace

if(gameObject.tag == "Tec")

if(child.gameObject.tag == "Tec")

和替换

if(gameObject.tag == "Awp")

if(child.gameObject.tag == "Awp")