如何忽略对象之间的碰撞 [英] How to Ignore Collision between Objects

查看:97
本文介绍了如何忽略对象之间的碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1.我编写了一个脚本,该脚本使用ontriggerenter查找在2D游戏中大炮何时击中另一艘船.

1.I have written an script that uses ontriggerenter to find when a cannon ball hits another ship in a 2D game.

private void OnTriggerEnter(Collider other)
{


    if (other.tag != "cannonBallWASD" )
    {
        return;
    }

    Destroy(other.gameObject);
    Destroy(gameObject);
    Debug.Log(other);
}

2.使用WASD(ShipWASD)控制的飞船向使用箭头键(ShipArrows)控制的第二艘船发射炮弹(cannonballWASD).该代码位于ShipArrows附带的脚本上.

2.The Ship that is controlled using WASD(ShipWASD) fires a cannonball(cannonballWASD) at a 2nd ship controlled using the arrow keys(ShipArrows). This code is on a script attached to ShipArrows.

3.我有一些单独的代码,这些代码使用没有网格渲染器(边界)的多维数据集,该渲染器会破坏任何离开边界的炮弹,使其看上去掉入海中.这是一个2人游戏.

3.I have a seperate bit of code in place which uses a cube without a mesh renderer(The Boundary) which destroys any cannonballs that leave the boundary so it looks like they are falling into the sea. This is a 2-PLAYER GAME.

4.问题出在这里-边界随船一起移动,这是ShipArrows的孩子.当我发射大炮时,炮弹击中了边界,而不是飞船对撞机.当我移除边界时,它会按原样击中飞船,但边界是我想要的东西.

4.Here in lies the problem- So that the Boundary moves around with the ship it is a CHILD of the ShipArrows. When I fire the cannon the cannonball hits the boundary instead of the ships collider. When i remove the boundary it hits the ship as it should but the boundary is something i want.

5.边界和飞船上都需要有触发器碰撞器".有什么方法可以忽略此脚本中的边界,但不能忽略以下脚本中的边界.

5.The boundary and the ship both have and need Is Trigger Colliders on them. Is there any way to ignore the boundary in this script but not ignore it in the following script.

private void OnTriggerExit(Collider other)
{
    if(other.tag != "cannonBallWASD" && other.tag != "cannonBallArrows")
    {
        return;
    }
    Destroy(other.gameObject);
}

此脚本使ShipArrows炮弹在离开ShipArrows边界时将其摧毁,从而使它们看起来像是掉入海中. (两艘船都这样做.)

This script makes ShipArrows cannonBalls look like they are falling in the sea by destroying them when they leave ShipArrows Boundary. (Both ships do this.)

推荐答案

要忽略冲突,请使用

To ignore collision, use Physics.IgnoreCollision.

还有 Physics.IgnoreLayerCollision 用于忽略碰撞层.

There is also Physics.IgnoreLayerCollision which is used to ignore collisions on layers.

只需将要忽略的所有对象放在一个层中,然后调用该函数以忽略它们上的层.

Just put all the Objects you want to ignore in a layer then invoke the function to ignore layers on them.

忽略Collison 3D :

Physics.IgnoreCollision(yourFirstCollider, yourSecondCollider, true)

Physics.IgnoreLayerCollision(yourFirstLayer, yourOtherLayer, true);

重置/识别Collison 2D :

Physics2D.IgnoreCollision(yourFirstCollider, yourSecondCollider, false);

Physics2D.IgnoreLayerCollision(yourFirstLayer, yourOtherLayer, false)


两层不必相同.您可以忽略来自不同层的对象上的碰撞.这也可以通过不带代码的编辑器来完成,方法是分配每个GameObject层并转到 Edit -> Project Settings -> Physics - ->或编辑-> 项目设置-> Physics 2D ,然后配置哪些层应该从那里相互碰撞.


Both layers do not have to be the-same. You can ignore collision on Objects from different layers. This can also be accomplished through the Editor without code by assigning each GameObject layer and going to Edit --> Project Settings --> Physics --> or Edit --> Project Settings --> Physics 2D then configure which layers should collide with one another from there.

这篇关于如何忽略对象之间的碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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