- Home /
Destroy Projectile on Impact?
I'm a noob making a disc golf game. I think I'd have trouble getting the discs to stick in the chains on impact, so I'd like to just destroy the discs on impact with the chains. The chains have a sphere collider and it's marked as a trigger. I also have an explosion set to detect collision. The collision is working.
However, I'm having a hard time figuring out how to simply destroy the discs. All discs are tagged as "discs". They're defined in a different script. Here is my code:
var explosion : Transform;
function OnTriggerEnter(hit : Collider)
{ if(hit.gameObject.tag == "discs") {
Destroy gameObject.tag == "discs";
var exp = Instantiate(explosion, gameObject.transform.position, Quaternion.identity);
StrokeCount.STROKES = 0;
StrokeCount.BASKETHITS += 1;
}
}
Trust me, I understand that I'm noob. I appreciate any help that's offered.
O$$anonymous$$, here's a little update/change of attack...
I created a different script entirely and oriented it as the kill disc on collision script. I've attached this to all discs.
function OnCollisionEnter(collision : Collision) {
if(collision.gameObject.tag == "chains")
{
Destroy(gameObject);
}
}
This doesn't destroy the disc and I've got to be missing something simple in my logic. I can get the disc to destroy when it collides with anything, but I'm having trouble getting it to ONLY destroy it when it collides with a specific collider, which is tagged "chains".
I appreciate the help so far. It's helped me understand why my previous method wasn't working.
Answer by aldonaletto · Apr 27, 2012 at 01:39 PM
If your original script was attached to the disc, it should be:
var explosion : Transform;
function OnTriggerEnter(hit : Collider){ if (hit.tag == "chain") { // if hit a chain object... Destroy(gameObject); // destroy himself... // and instantiate the explosion in its place: var exp = Instantiate(explosion, transform.position, Quaternion.identity);
StrokeCount.STROKES = 0; StrokeCount.BASKETHITS += 1; } } Your second script will not work unless you uncheck Is Trigger in the chain collider: OnCollision events only occur when the rigidbody hits a regular collider - OnTrigger events are reported when the collider has Is Trigger checked.
Your disc script could be something like this:
var explosion : Transform;
function OnCollisionEnter(col : Collision){ if (col.gameObject.tag == "chain") { // if hit a chain... Destroy(gameObject); // destroy itself // instantiate the explosion at its own position: var exp = Instantiate(explosion, transform.position, Quaternion.identity);
StrokeCount.STROKES = 0; StrokeCount.BASKETHITS += 1; } }
Answer by kolban · Apr 27, 2012 at 01:25 AM
Hi there and welcome. The Unity Answers forum is more for focused Q&A on Unity as opposed to general discussion topic so you may want to look at the Unity Community forums for questions such as this. Looking at your code, I see that you have a line which reads:
Destroy gameObject.tag == "discs";
I am actually surprised this even compiles. The syntax for the Destroy command can be found here:
http://unity3d.com/support/documentation/ScriptReference/Object.Destroy.html
As you will see, it expects an Object as a parameter and this is commonly an instance of GameObject to be destroyed. In your code, you are passing in:
gameObject.tag == "discs"
Which is a "boolean" (a true/false value) and most certainly not an object. As such, your discs are not disappearing.
It actually doesn't compile, I just wanted to show my train of thought. Sorry for not mentioning that. I can't seem to write a Destroy that works unless it's actually destroying the chains, not the disc.
From your answer, I do understand why it's not working, though.
Answer by McDardy · Apr 27, 2012 at 08:36 AM
From what I understand this destroying projectiles on impact is very similar behaviour to shooting rockets that explode when colliding, right?
http://unity3d.com/support/resources/tutorials/fpstutorial.html
I don't know why this tutorial is hided for people. It's pretty old but when you're searching for some ideas it will do... And some colliding/destroying things you can also find in... Lerpz 3D platform tutorial:
http://unity3d.com/support/resources/tutorials/3d-platform-game
I found also this:
http://unity3d.com/support/documentation/ScriptReference/Object.Instantiate.html
And I guess that will be the best resource for your problems. It's overcommented so noone should have problems with understanding and implementing this in their projects.
Your answer