Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
215 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...