- Home /
Instantiate diffent particle for different object
Hello there , I want my bullet to have different particles when it hits different tagged object. Here is my script , but it's not working , and I don't know why!
//Particles
var HitConcrete : GameObject;
var HitMetal : GameObject;
function LateUpdate()
{
var fwd = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;
if (HitConcrete)
{
if (hit.rigidbody == "Concrete")
var HitConcrete1 = Instantiate(HitConcrete, hit.point + (hit.normal * HitParticleSpacing), Quaternion.LookRotation(hit.normal));
}
if (HitMetal)
{
if (hit.rigidbody == "Metal")
var HitMetal1 = Instantiate(HitMetal, hit.point + (hit.normal * HitParticleSpacing), Quaternion.LookRotation(hit.normal));
}
}
Answer by TheValar · Nov 06, 2013 at 05:23 PM
It seems like you are trying to use a raycast but you are not doing it correctly.
That being said as long as your bullet, concrete wall, and metal wall all have colliders you don't need to use a raycast.
just use
void OnCollisionEnter(Collision collision) {
if(collision.gameobject.tag=="Concrete"){
//do something
}
else if(collision.gameobject.tag=="Metal"){
//do something
}
}
NOTE: this is untested code and is just to give you the basic idea.
And what about instantiating the particle effect without RayCast? I used RayCast because I wanted the particle effect to be spawned exactly where the bullet hits ;)
Would it be precise enough to just spawn the particle effect at the location of the bullet when it hits? Also the Collision class has an array of contact points http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html so if your bullet has a sphere or capsule collider you could probably get preciscion very close to a raycast
$$anonymous$$y problem with the script was that I have 2 object with box collider , one tagged as $$anonymous$$etal , and the other one tagged as Concrete. Also my bullet has a box collider. By the way if I hit these object it plays both particles effect, not just the "Concrete particle" on the object tagged Concrete.... the same for the object tagged $$anonymous$$etal. Whate can I do to make the Particle Effect recognizes which object it hit?
I'm assu$$anonymous$$g that the metal and concrete objects are totally separate with there own colliders. Unless the bullet is colliding with both of these objects' colliders then only one effect should play.
Yes , they are totally separate and both have they own colliders. But he recognition seems not to work :/
Your answer
Follow this Question
Related Questions
Particle Instantiation problem. 0 Answers
Stop raycasting for close distance 1 Answer
Destroying object using his name and raycast 2 Answers
Why cant this work 1 Answer
different particle on enemy second hit 0 Answers