- Home /
Multiple Bullet Hit Effect
Hi guys I need of your help again. I have a bullet Prefab with a bullet script. Now what i'm working on is this: If the bullet hit something with the PhysicMaterial Metal then it should Instantiate a MetalHitEffect, if the bullet hit something with the PhysicMaterial Wood then it should Instantiate a WoodHitEffect and if the bullet hit something with the PhysicMaterial Terrain then it should Instantiate a TerrainHitEffect. Now i know that this can be made in the OnCollisionEnter function, but i don't know how can i get the PhysicMaterial of the collision :S. If you know the solution or can anyone help me understand this it will be valued. Thanks, bye.
Answer by Berenger · Jun 06, 2012 at 03:41 PM
You can access the physic material like that : collider.material. However, you can't really tell which is wood and which is metal, nuless you test if friction is to some threshold. And if you ever want to add a third material, or change that threshold, things get complicated.
You should use a tag, or a layer, or a custom class that contains an enum Wood, Metal etc if you need tags/layer for something else.
uhm i've tried this way. I've declarated 3 vars: var $$anonymous$$etal$$anonymous$$aterial : Physic$$anonymous$$aterial; var Wood$$anonymous$$aterial : Physic$$anonymous$$aterial; var Ground$$anonymous$$aterial : Physic$$anonymous$$aterial; and 3 vars for the corresponding HitEffect like GameObject. in the OnCollisionEnter ( collision : Collision) function i've writed this : if(collision.collider.material == $$anonymous$$etal$$anonymous$$aterial) { Instantiate($$anonymous$$etalHit,contact.point,rotation); } and the same for the other 2 materials (contact and rotation are 2 variables that i've declarated under the OnCollisionEnter function)
I usually don't trust the default Equals functions (==) to compare complicated object, even more for material as they are copied at start. That's probably your issue.
uhm, ok i'll use Tags or something like Layer and i try to fix my problem. Thanks a lot for the help :) bye
Your answer
Follow this Question
Related Questions
How to make my bullets spread when fired 3 Answers
scripting question 3 Answers
Turret bullet rotation problem 1 Answer
Fast bullet ignor collision sometimes 3 Answers
Turn off function 2 Answers