Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by DanSparrow · Feb 01, 2017 at 12:41 AM · collisionparticlesparticlesystembulletplay

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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bubzy · Feb 01, 2017 at 02:42 AM 0
Share

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,

avatar image DanSparrow bubzy · Feb 01, 2017 at 03:05 AM 0
Share

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 :/

avatar image bubzy DanSparrow · Feb 01, 2017 at 08:33 AM 0
Share

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();
  }
Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

125 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

accessing the colliding particle 0 Answers

Particle System Collision Mode 1 Answer

How do I make my particle system detect/mark a floor collision? 0 Answers

Can I shoot a ray from a particle that hit a collider? 1 Answer

Spawn particles on collider surfaces in range 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges