根据对象标签Unity 2d禁用或启用碰撞 [英] Disable or enable collisions based on object tag Unity 2d

查看:560
本文介绍了根据对象标签Unity 2d禁用或启用碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想基于其标签在统一2d游戏中禁用或启用与对象的碰撞.可以说我有一个带有标签"foo1"的对象和一个带有"foo2"标签的对象.如果用户选择与对象"foo1"相撞,则它不应与对象"foo2"相撞.

I want to disable or enable collision with object in unity 2d game based on its tag. Lets say I have object with tag "foo1" and objects with tag "foo2". If user choose to collide with objects "foo1" then it should not collide with objects "foo2".

我该如何实现?我试过了:

How could I achieve this? I tried this:

 void OnCollisionEnter(Object other)
 {
      if (other.tag == "foo1")
          collider.enabled = false;
 }

但这不起作用有两个原因.第一个对象必须将isTrigger设置为true(这不能设置为用作地面的对象),如果我禁用整个碰撞器,则对象将掉落到地面.

But this is not working for two reasons. First object has to have isTrigger set to true (this could not be set for objects that serves as ground) and if I disable entire collider then object will fall through ground.

我是团结的新手,我将更详细地研究它,但是我想寻求快速帮助,也许想知道如何做到这一点?

I am new to unity and I will study it in more details but I am asking for quick help and maybe idea how to do this?

推荐答案

除了通过标签执行此操作外,您可能还想

Instead of doing this via tags, you might want to have a look at layers.

通过将不同的对象分配给不同的图层,可以将它们设置为彼此碰撞或忽略任何潜在的碰撞.您可以在以下位置实现

By assigning different objects to different layers, you can set them to either collide with each other, or ignore any potential collisions. You can achieve this at

编辑->项目设置->物理

Edit->Project Settings->Physics

您可以在其中编辑图层碰撞矩阵,以启用或禁用图层元素之间的碰撞.

where you can edit the layer collision matrix, to enable or disable collisions between layer elements.

这篇关于根据对象标签Unity 2d禁用或启用碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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