Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
225 views
in Technique[技术] by (71.8m points)

c# - How to Ignore Collision between Objects

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...