我如何让一些对撞机忽略统一3D中的触发器 [英] How do I get some colliders to ignore triggers in unity 3D

查看:97
本文介绍了我如何让一些对撞机忽略统一3D中的触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在和我做一名第一人称射击游戏,玩家是在与僵尸作战.因此,使用此功能,我在地图周围散布了弹药箱:

I'm currently making a first person shooter with me, the player, fighting against zombies. So I have ammo boxes scattered around the map with this function:

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"网格碰撞器;但我遇到的问题是我的僵尸也能够拿起弹药.这在我的脚本中带来了其他一些问题.那么有什么方法可以阻止僵尸捡起弹药吗?僵尸网格对撞机不是正在触发";而是但它仍然可以拿起弹药.

This code works great as I have a mesh collider on my character that is "Is trigger" and "convex"; but the problem I'm having is that my zombie is also able to pick up the ammo. This creates several other problems in my scripts. So is there any way to stop the zombies from being able to pick up the ammo? The zombie mesh collider is not "Is trigger"; but it can still pick up the ammo.

推荐答案

解决问题的最简单方法是利用注释中提到的标签.在您的玩家游戏对象上添加一个玩家"标签,并将您的弹药拾取代码包装在if语句中,如下所示:

The easiest way to solve your problem would be to utilize tags as mentioned in the comments. Add a "player" tag to your player gameobject and wrap your ammo pickup code in an if statement as shown below:

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);
        }
    }
}

要回答书面问题,您可以通过修改层碰撞矩阵来忽略某些碰撞.例如,您可以在弹药盒上应用一个名为"pickup"的图层,并在您的僵尸上应用一个称为敌人"的图层.然后,您可以修改图层碰撞矩阵,以使敌人图层和拾取图层不会相互作用.

To answer the question as written: you can have certain collisions ignored by modifying the layer collision matrix. For example, you could apply a layer to your ammo boxes called "pickup" and a layer to your zombies called "enemy". You could then modify your layer collision matrix so that the enemy and pickup layers do not interact.

这篇关于我如何让一些对撞机忽略统一3D中的触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆