Can't find a way to add particle effects to a collision
And before you tell me to use google or to search in the web, let me tell you I've been researching about this for about 3 hours and I simply can't find an answer. All I find is old topics that work with the old particle system and we all know most of that code is obsolete now.
I'm kinda new with Unity so if you could be quite specific, even better. My problem is this:
I have 3 main scripts for this, one for the enemy health, one for the player weapon and one for the action of firing itself. In enemy health I specify enemy's status and I use OnCollisionEnter to deal damage when my bullet hits, with a reference to my player weapon script for damage and other data. The firing scripts basically instanciates the bullet with a rigidbody component and adds the movement.
Now I can't find a way to simply play an animation in the spot where the bullet hits when it collisions with the enemy. That's all I want and I can't get to understand how to work with ParticleSystem, GetComponent and "Play()" to make it work as I read here and there.
This is my code to instanciate the bullet itself:
void Shoot()
{
shot = Instantiate(shotEmitter) as GameObject;
shot.transform.position = transform.position + Camera.main.transform.forward * 2;
Rigidbody rb = shot.GetComponent<Rigidbody>();
rb.velocity = Camera.main.transform.forward * 40;
Destroy(shot, 2f);
}
And to cause the damage and destroy the bullet on contact:
void OnCollisionEnter(Collision _col)
{
if (_col.collider.gameObject.tag == "Bullet")
{
GameObject theDamage = GameObject.Find("Gun");
PlayerWeapon damageFromWeapon = theDamage.GetComponent<PlayerWeapon>();
currentEnemyHealth -= damageFromWeapon.weaponDamage;
WeaponFiring theShot = theDamage.GetComponent<WeaponFiring>();
Destroy(theShot.shot);
}
}
Is there any way to add a particle system (I acutally created one in an empty object and all) to the point where it hits? If you need any other info let me know.
cant you add one to the bullet like
private ParticleSystem impactParticle;
void Setup()
{
impactParticle = gameObject.GetComponent<ParticleSystem>();
}
OnCollisionEnter()
{
impactParticle.Play();
}
? of course this is assu$$anonymous$$g the bullet has a particlesystem attached to it. regarding the position of the particle effect, with a bullet you can probably just play it, as it should be at the bullets current position,
The time I tried that, and after assigning the particle system I created in the inspector to the script, it kept saying there was no particle system or that I had to check if there was, but I don't remember how to check for a particle system to be able to play it
EDIT: Tried adding "if (impactParticle != null)" and it won't give that error, but won't play any particle effect anyway :/ I have made a script for the particle system, and attached it to the bullet, but doesn't seem to work :/
this might be bad practice, but ive tried it another way that works for me, i've added the particle system as a child of my object then made it a public variable so i can drag it in in the editor.
maybe you'll have some luck with that?
public ParticleSystem impactParticle;
void Setup()
{
// impactParticle = gameObject.GetComponent<ParticleSystem>();
}
OnCollisionEnter()
{
impactParticle.Play();
}
Answer by CybernetHacker14 · Oct 05, 2017 at 06:23 AM
You can create a prefab as an empty gameObject with a particle system attached to it, with necessary settings as per your wish. When your bullet hits the collider, which I think you have mentioned in your second part of code, just before destroying the bullet, you can instantiate the particle system prefab at the position of the co-ordinate, then destroy the bullet and afterwards destroy the prefab so as not to hog up memory. I am sorry as I currently don't have any code for it, but it should be easy for you to figure it out. I think most of this functionality will happen just before Destory(theShot.shot)
Answer by JusSumGuy · May 22, 2018 at 06:24 PM
> ***public GameObject explosion; // I'm Guessing it's an explosion you want to show***
> void OnCollisionEnter(Collision _col)
> {
> if (_col.collider.gameObject.tag == "Bullet")
> {
> GameObject theDamage = GameObject.Find("Gun");
> PlayerWeapon damageFromWeapon = theDamage.GetComponent();
> currentEnemyHealth -= damageFromWeapon.weaponDamage;
> WeaponFiring theShot = theDamage.GetComponent();
***Instantiate(explosion, transform.position, Quaternion.Identity);***
> Destroy(theShot.shot);
> }
> }
Then on the explosion gameobject you can add your animation component or your particle system, then after the explosion gameobject is instantiated you will need to destroy it. So add a script to the explosion gameobject like so :
int destroyTime = 2f;
> Start(){
> Destroy(gameObject, destroyTime);
> }
PS: This is a place meant for people to ask questions about this stuff. If anyone gives you a hard time telling you to use google don't pay them no mind. Many if not most people on here are very helpful :). Hope this helps!