我目前正在与我(玩家)对抗僵尸战斗。因此,使用此功能,我在地图周围散布了弹药箱:
void OnTriggerEnter(Collider other)
{
AmmoSound.Play();
if (Ammo_count.LoadedAmmo == 0)
{
Ammo_count.LoadedAmmo += 10;
this.gameObject.SetActive(false);
}
else
{
Ammo_count.CurrentAmmo += 10;
this.gameObject.SetActive(false);
}
}
该代码非常有用,因为我的角色上有一个“ Is trigger”和“ convex”网格碰撞器;但是我遇到的问题是我的僵尸也能够拿起弹药。这在我的脚本中带来了其他一些问题。那么有什么办法可以阻止僵尸捡起弹药吗?僵尸网格对撞机不是“正在触发”;而是但它仍然可以拿起弹药。
答案 0 :(得分:3)
解决问题的最简单方法是利用注释中提到的标签。在您的玩家游戏对象上添加一个“玩家”标签,并将您的弹药拾取代码包装在if语句中,如下所示:
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "player")
{
AmmoSound.Play();
if (Ammo_count.LoadedAmmo == 0)
{
Ammo_count.LoadedAmmo += 10;
this.gameObject.SetActive(false);
}
else
{
Ammo_count.CurrentAmmo += 10;
this.gameObject.SetActive(false);
}
}
}
要回答书面问题,您可以通过修改layer collision matrix来忽略某些冲突。例如,您可以在弹药盒上应用一个名为“ pickup”的图层,并在您的僵尸上应用一个称为“敌人”的图层。然后,您可以修改图层碰撞矩阵,以使敌人图层和拾取图层不会相互作用。