(Unity) 武器切换,切换后销毁之前的武器
(Unity) weapon switching, destroying previous weapon after switch
当我切换武器时,旧武器不会被摧毁,所以你最终会在屏幕上看到 2 把可以使用的枪。我曾尝试使用 Destroy(currentWeapon) 销毁游戏对象,但没有成功。此外,我使用一个空的游戏对象附加到玩家预制件,这是枪支被实例化的地方。问题是我需要 2 个不同的游戏对象,所以不同的枪可以放在不同的位置。我已经制作了两个空的游戏对象,但我需要帮助让它们在切换枪支时切换。
非常感谢任何帮助!
using UnityEngine;
using UnityEngine.Networking;
public class WeaponManager : NetworkBehaviour {
[SerializeField]
private string weaponLayerName = "Weapon";
[SerializeField]
private Transform tecweaponHolder;
[SerializeField]
private Transform awpweaponHolder;
[SerializeField]
private PlayerWeapon primaryWeapon;
[SerializeField]
private PlayerWeapon secondaryWeapon;
private PlayerWeapon currentWeapon;
private WeaponGraphics currentGraphics;
void Start ()
{
EquipWeapon(primaryWeapon);
}
void Update()
{
if(Input.GetAxis("WeaponSwitch") >0f)
{
EquipWeapon(secondaryWeapon);
Debug.Log("switched weapon");
}
else if (Input.GetAxis("WeaponSwitch")<0f)
{
EquipWeapon(primaryWeapon);
Debug.Log("switched weapon back");
}
}
public PlayerWeapon GetCurrentWeapon ()
{
return currentWeapon;
}
public WeaponGraphics GetCurrentGraphics()
{
return currentGraphics;
}
void EquipWeapon (PlayerWeapon _weapon)
{
currentWeapon = _weapon;
GameObject _weaponIns = (GameObject)Instantiate(_weapon.graphics, tecweaponHolder.position, tecweaponHolder.rotation);
_weaponIns.transform.SetParent(tecweaponHolder);
currentGraphics = _weaponIns.GetComponent<WeaponGraphics>();
if (currentGraphics == null)
Debug.LogError("No WeaponGraphics component on the weapon object: " + _weaponIns.name);
if (isLocalPlayer)
Util.SetLayerRecursively(_weaponIns, LayerMask.NameToLayer(weaponLayerName));
}
}
更新:
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");
}
}
在您的代码中,您实际上没有销毁旧的(当前的)武器。
void EquipWeapon(PlayerWeapon _weapon) {
foreach (Transform child in tecweaponHolder) {
Destroy(child.gameObject);
}
currentWeapon = _weapon;
// ...
}
当我切换武器时,旧武器不会被摧毁,所以你最终会在屏幕上看到 2 把可以使用的枪。我曾尝试使用 Destroy(currentWeapon) 销毁游戏对象,但没有成功。此外,我使用一个空的游戏对象附加到玩家预制件,这是枪支被实例化的地方。问题是我需要 2 个不同的游戏对象,所以不同的枪可以放在不同的位置。我已经制作了两个空的游戏对象,但我需要帮助让它们在切换枪支时切换。
非常感谢任何帮助!
using UnityEngine;
using UnityEngine.Networking;
public class WeaponManager : NetworkBehaviour {
[SerializeField]
private string weaponLayerName = "Weapon";
[SerializeField]
private Transform tecweaponHolder;
[SerializeField]
private Transform awpweaponHolder;
[SerializeField]
private PlayerWeapon primaryWeapon;
[SerializeField]
private PlayerWeapon secondaryWeapon;
private PlayerWeapon currentWeapon;
private WeaponGraphics currentGraphics;
void Start ()
{
EquipWeapon(primaryWeapon);
}
void Update()
{
if(Input.GetAxis("WeaponSwitch") >0f)
{
EquipWeapon(secondaryWeapon);
Debug.Log("switched weapon");
}
else if (Input.GetAxis("WeaponSwitch")<0f)
{
EquipWeapon(primaryWeapon);
Debug.Log("switched weapon back");
}
}
public PlayerWeapon GetCurrentWeapon ()
{
return currentWeapon;
}
public WeaponGraphics GetCurrentGraphics()
{
return currentGraphics;
}
void EquipWeapon (PlayerWeapon _weapon)
{
currentWeapon = _weapon;
GameObject _weaponIns = (GameObject)Instantiate(_weapon.graphics, tecweaponHolder.position, tecweaponHolder.rotation);
_weaponIns.transform.SetParent(tecweaponHolder);
currentGraphics = _weaponIns.GetComponent<WeaponGraphics>();
if (currentGraphics == null)
Debug.LogError("No WeaponGraphics component on the weapon object: " + _weaponIns.name);
if (isLocalPlayer)
Util.SetLayerRecursively(_weaponIns, LayerMask.NameToLayer(weaponLayerName));
}
}
更新:
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");
}
}
在您的代码中,您实际上没有销毁旧的(当前的)武器。
void EquipWeapon(PlayerWeapon _weapon) {
foreach (Transform child in tecweaponHolder) {
Destroy(child.gameObject);
}
currentWeapon = _weapon;
// ...
}