I would like a small impact particle effect to appear wherever a laser bullet hits, but I dont know how to do this. My lasers are simply sprites with colliders/rigidbody’s attached along with a script which moves them along the Z axis.
Use sub-emitters within your particle effects, if you create one there’s an option there to have your particle start on collision, check out the sub emitters module section in the link I gave you, there’s also other handy stuff like on death and so on so if you want to make fireworks or rockets and such that’s what I would use them for.
When I try this it says (to paraphrase) because the sub emitter is part of a prefab it cant be done.
After watching the video and looking at the example script I updated the weapon script attached to my laser bolt so it now looks like this:
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Enemy")
{
GetComponent<EnemyHealth>().TakeDamage (10);
ContactPoint contact = collision.contacts[0];
Quaternion rot = Quaternion.FromToRotation(Vector3.up, contact.normal);
Vector3 pos = contact.point;
Instantiate(explosionPrefab, pos, rot);
Destroy(gameObject);
return;
}
But my laser bolts now pass through my enemies, they don’t have any effect?
bump
I’m glad you bumped this, because I have been experimenting with bullet spread and making fancier and more advanced bullet spread mechanics and so on and using particle effects like what you wanted to do. Now I managed to solve the problem of collision there’s a tab you can click called collision in the options of the particle effects so it will bounce off colliders or hit them and so on.
However we now have a problem as you say of actually getting the damn things to interact, because we’ve got no 3D object that physically hits the other objects to interact with them that means you can’t knock anything over or cause them to get pushed away with any force.
If anyone wants to chime in I’d be grateful too, I tried all sorts of ideas like using animations to mimic bullet spread but I don’t want to have to rely on shooting actual 3D objects in order to make the bullets collide because obviously that would mean it’s not only limited graphically but quite expensive resources wise as well.
I’ll see if I can find any documentation or answers on this elsewhere but someone might know already and have an up to date answer.
Edit: Okay, I think I have a possible solution, but I’m still thinking about it, I saw mention of what’s called the legacy particle system and it seems to be what we’re both looking for, it looks like though I’m going to have to search up code to get it all working right.
I came across something promising, it’s to do with something called the World Particle Collider, this enables along with some other options exactly what we want, the ability for particle effects to have their own colliders and interact with other objects. There’s just one problem left though and I still haven’t managed to get them interacting with rigidbody gameobjects properly, there must be an option somewhere I’ve missed that allows particles to have some sort of force to them to make a rigidbody move.
Adding the world particle component and just following the green highlighted answer should fix most of your collision issues, I’m still searching myself for a solution to the problem of adding force.
I’m taking a look at the code here now and trying to understand it so I can get it to work, I think this is the solution to the issue of making rigidbodies react to the particle effects.
IT WORKS! Yay! I worked it out just by thinking about it!
Okay, so here’s a run down of what to do now Serbusfish.
. Follow the answer on the World Collider Particle system, get your settings right for your particle system generally
. Copy and paste the code in the OnParticleCollision documentation that I posted up for you into a new C# script, I just used the first sample code given
. Add the OnParticleCollision script to your Particle System that you want to act as the bullets coming from your gun
. If the rigidbodies aren’t moving when you point your particle effects at them you should just need to change the your ‘addforce’ amount to something higher and then it should be fine, adjust according to what you want personally and how it reacts in your scene
Let me know if this all works, you should have no problems making randomised bullets and so on now and keeping the nice effects you have for your guns. Looking at this code, you could also modify it to make proper health damage depending on whether your particles hit or not it would just be simply a matter of making the OnParticleCollision code send a message to take away a certain amount of health on each hit.
Sorry for the late response, I havent been on the forums much recently. I will have a look into your method, thank you, however in my absence I have found a simple alternative solution:
public GameObject impactParticle;
public Vector3 impactNormal;
void OnTriggerEnter(Collider hit)
{
if (hit.gameObject.tag == "Enemy")
{
hit.GetComponent<EnemyHealth>().TakeDamage (10);
impactParticle = Instantiate(impactParticle, transform.position, Quaternion.FromToRotation(Vector3.up, impactNormal)) as GameObject;
Destroy(gameObject);
return;
}
}
This isnt perfect though, depending on the game object in question the impact sometimes instantiates in the wrong place. Is there a way to set a custom vector3 for the transform as is done with the Quaternion rotation?
I have solved the problem! The above script I posted works fine, the only problem is the particle effect will be instantiated in the centre of the enemy object, which doesnt look right. The solution is to simply offset the particle effect within its game object. For me a -2 on the Z axis was the right amount. You can see how it looks in this short gameplay clip I just uploaded:
Oh nice! I’m glad that worked, you see I was showing you that method because it seemed to be the most accurate for particle effects but if this works fine for you then great, I still recommend getting a World Particle Collider set up and so on for the sake of accuracy if you haven’t already, hope your game does well.
I need help guys… i have added that effect in my character but when my character get hits the effect is generating on the ground i want that effect to be generated over the character head how i will move it up?