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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…