How to Ignore Collision between Objects

前端 未结 1 629
一个人的身影
一个人的身影 2020-12-12 05:56

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)
{
         


        
1条回答
  •  时光说笑
    2020-12-12 06:16

    To ignore collision, use Physics.IgnoreCollision.

    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.

    Ignore Collison 3D:

    Physics.IgnoreCollision(yourFirstCollider, yourSecondCollider, true)
    

    or

    Physics.IgnoreLayerCollision(yourFirstLayer, yourOtherLayer, true);
    

    Reset/Recognize Collison 2D:

    Physics2D.IgnoreCollision(yourFirstCollider, yourSecondCollider, false);
    

    or

    Physics2D.IgnoreLayerCollision(yourFirstLayer, yourOtherLayer, false)
    

    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.

    0 讨论(0)
提交回复
热议问题