• 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 /
This question was closed Apr 19, 2013 at 05:49 PM by AlucardJay for the following reason:

Duplicate Question : http://answers.unity3d.com/questions/440351/how-to-add-explosion-after-shot.html

avatar image
0
Question by Purpleshine84 · Apr 18, 2013 at 08:02 PM · explosionshot

How to add explosion after shot

Hello guys, hear me out (the scripting is all in Javascript):

         Shotgun Script (for the gun)
 public var bulletPrefab : Transform;
 public var bulletSpeed : float = 6000;
 var ammo : int = 60;
 var gunShot : AudioClip;
 var reload : AudioClip;
 var emptyClip : boolean = false;
 
 function Update () {
     if(Input.GetButtonDown("Fire1")) {
         if(ammo > 0) {
             Shoot();
         }
         else
         {
             return;
         }       
     }
     if (Input.GetKeyDown (KeyCode.R)) {     
         if (emptyClip) {
             Reload();            
         }
     }
     if (ammo > 0) {
         emptyClip = true;
     }
     else
     {
         emptyClip = false;
     }
 }        
  
 function Shoot() {   
     var bullet = Instantiate(bulletPrefab, transform.Find("Bulletspawn").position, transform.Find("Bulletspawn").rotation);
     bullet.tag = "BulletspawnProjectile";
     bullet.rigidbody.AddForce(transform.forward * bulletSpeed);
     audio.PlayOneShot (gunShot) ;
     ammo--;
 }                 
 
 function Reload() {
     audio.PlayOneShot(reload);    
     ammo = 60;
 }
 
 function onGUI() {  
     GUI.Label (Rect ( 0,0, 75, 25), "Ammo " + ammo) ;
 }


                 For the Turret

     public var lifeTime : int = 1;
     var explosion : Transform;
     
     function OnTriggerEnter () {
    Destroy(gameObject, lifeTime);
    }
     
 
     function OnTriggerEnter(hit : Collider) 
     {
       if(hit.gameObject.tag == "BulletspawnProjectile")
     {
     Destroy(hit.gameObject);
     var exp = Instantiate(explosion, gameObject.transform.position, Quaternion.identity);
     Destroy(gameObject);
     }
     }  

So: I installed the First person Controller, and attached a gun, then I parented those. After that, I made a Bulletspawn (sphere) and attached that to the gun. Then: for the Bullet, I made a prefab, which I used on the script for the gun. So, this allows me to shoot some bullets. Then I created the Turret, and the Turret is shooting somestuff too, also with the same BulletPrefab and via a Bullet(Turret)spawn. Now, I want to make an explosion after my bullets have killed the Turret. I tried with tag, but it doesnt seem to work, I tried with: bulletPrefab, Bulletspawn, gunprojectile etc etc (as a tag in tag manager) and change that in the scripts, but nothings seems to work and I get some error, for example that it "cannot fine the first person controller to define as tag". Somethins like this, I am still a beginner and I guess I am doing something wrong, like I am not seeing the major (but simple) isseu of the solution here....

Comment
Add comment · Show 19
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 Turbine · Apr 18, 2013 at 08:12 PM 0
Share

There are probably 100 good tutorials on this topic and 10000 other questions like this.

avatar image Purpleshine84 · Apr 18, 2013 at 09:22 PM 0
Share

@Turbine Would be great if you could tell me where I can find the right answer on my question in one of those 100 good tutorials and 10000 related questions, since I looked already at 50 of them, or so and I couldnt find any proper outcome for what I want to achieve, thats the reason that I came here on this forum (non-coincidental)

avatar image Purpleshine84 · Apr 18, 2013 at 10:41 PM 0
Share

@jbarba_ballytech Thanks for that idea, though, look on line 51, thats the script for the Turret where I try to implement the explosion.

avatar image Purpleshine84 · Apr 18, 2013 at 10:45 PM 0
Share

jbarba_ballytech Wow! Thanks a bunch! I am indeed a noob, but I will try this out and report it to you tommorow!

avatar image hoy_smallfry · Apr 18, 2013 at 11:15 PM 0
Share

No, I'm sorry, I just tried your code and it seems there is nothing wrong with what I mentioned before. I was under the impression it was all one script before because it was all one block.

But, now I think I know what might be the problem. So, all of it works up until the point that the bullet hits the turret, which gives me a hunch...

OnTriggerEnter only works if the collider is marked as IsTrigger; there should be a setting on the collider in the inspector that changes that. But keep in $$anonymous$$d that making it a trigger makes it so that physics doesn't resolve collisions for it, that is, it makes it so they "ghost" through things.

If you don't want this "ghost" behavior, you need to ins$$anonymous$$d use OnCollisionEnter. This will let it detect a regular collision.

Show more comments

0 Replies

  • Sort: 

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

14 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

Related Questions

How to add explosion after shot 2 Answers

FPS Tutorial 1-What do I drop "Explosion" onto in the inspector window for the "Missile" 3 Answers

Spawning an explosion using an IF statment 1 Answer

Make grenade apply force to rigidbodies around it 3 Answers

Grenade Eplosion 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