Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Purpleshine84 · Apr 18, 2013 at 01:25 AM · bulletexplosionshot

How to add explosion after shot

 public var lifeTime : int = 1;
 
 function OnTriggerEnter () {
    Destroy(gameObject, lifeTime);
    }



Hi guys, I created a 1st person controller with a gun, and its shooting and working fine, and killing my enemies but I want to end with an explosion after they are killed. So, I dont know exactly how to put this down in the script. Above you see my first version and after this text, my second version, it doesnt give too much error but it just isnt working... please help me.

 public var lifeTime : int = 1;
 var explosion : Transform;
 
 function OnTriggerEnter () {
    Destroy(gameObject, lifeTime);
    }
     
 
     function OnTriggerEnter(hit : Collider)
     {
       if(hit.gameObject == "Bulletspawn")
     {
     Destroy(hit.gameObject);
     var exp = Instantiate(explosion, gameObject.transform.position, Quaternion.identity);
     Destroy(gameObject);
     }
     }  
Comment
Add comment · Show 3
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 ExpiredIndexCard · Apr 18, 2013 at 01:32 AM 0
Share

Try changing explosion from transfOrm to a game object

avatar image Chronos-L · Apr 18, 2013 at 01:55 AM 1
Share

I don't think that matters. Instantiate() requires a Object as the parameter, not GameObject.

http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html

avatar image Purpleshine84 · Apr 18, 2013 at 04:13 AM 0
Share

Destroyed Bullet:

 public var lifeTime : int = 1.5;
 
 function Awake () {
     Destroy(gameObject, lifeTime) ;
 }

Damage Script:

 var damage = 10;
 //instant kill
 function OnCollisionEnter ( collisionInfo ) {
     collisionInfo.other.Send$$anonymous$$essage("ApplyDamage", damage, Send$$anonymous$$essageOptions.DontRequireReceiver) ;  
 } 

Turret Script:

 public var BulletSpeed : float = 6000;
 public var BulletPrefab : Transform;
 var LookAtTarget : Transform;
 var nextShotTime : float = 0.0;
 var timeBetweenShots : float = 2.0;
 var damp : float = 6.0;
 var gunShot : AudioClip;
 
 function Update ()
 {
     if(LookAtTarget)
     {
     var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
     transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
     
     if(nextShotTime <= Time.time)
     {
     Shoot();
     nextShotTime = Time.time + timeBetweenShots;
     }
     }
     
     }
     
     function Shoot(){
     var Bullet = Instantiate(BulletPrefab, transform.Find("TurretBulletSpawn").position, transform.Find("TurretBulletSpawn").rotation);
     Bullet.rigidbody.AddForce(transform.forward * BulletSpeed);
     audio.PlayOneShot(gunShot);
     }

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Chronos-L · Apr 18, 2013 at 02:01 AM

it doesnt give too much error, this makes me worries a little. Do you have any errors at all? Are those warnings and you take them as errors? Or you just trying to say that there are no errors at all?


From what I can see here, the only problem is this line:

 //you can't compare a gameobject with string
 if(hit.gameObject == "Bulletspawn")
 
 //Correction
 //Either this (less likely)
 if( hit.gameObject.name == "Bulletspawn" ) 
 
 //or this (more likely)
 if( hit.gameObject.CompareTag("Bulletspawn") )



One more thing, from this documentation, will function OnTriggerEnter () work?

Comment
Add comment · Show 15 · 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 Purpleshine84 · Apr 18, 2013 at 02:17 AM 0
Share

@Chronos-L I am not really much into scripting yet.. but I am working on it. $$anonymous$$y FPC shoots the Turret and its killed, so thats fine, it works with this code:

public var lifeTime : int = 1; function OnTriggerEnter () { Destroy(gameObject, lifeTime); }

avatar image Purpleshine84 · Apr 18, 2013 at 02:17 AM 0
Share

@Chronos-L But I want to let it explode, not just dissapear, because that just looks lame somehow..

avatar image Chronos-L · Apr 18, 2013 at 02:25 AM 0
Share

Does fixing the if( hit.gameObject == "BulletSpawn" ) fix the problem? If not, for your second script, try keeping the function OnTriggerEnter( hit : Collider ) and remove the function OnTriggerEnter().

avatar image Purpleshine84 · Apr 18, 2013 at 02:31 AM 0
Share

@Chronos-L I am not sure what you mean, I already tried what you wrote above and nothing seems to happen. What do I have to do with "if" and what do I have to try with function OnTriggerEnter? If I remove it it gives a huge error..:(

avatar image Chronos-L · Apr 18, 2013 at 02:52 AM 0
Share

From your code, you will instantiate a explosion if the condition is right. So the problem now should be your setup.

Is this script attached to a enemy or a bullet? And what errors you are getting? What does your current script look like?

Show more comments
avatar image
1

Answer by AlucardJay · Apr 19, 2013 at 05:37 PM

Why do you have 2 trigger event functions on your turret? Consider the logic, try to read the code as the machine reads it.

There is a command called Invoke : http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.Invoke.html

With that you can call a function after a set amount of time :

 public var lifeTime : int = 1;
 var explosion : Transform;
 var explosionSound : AudioClip;
 
 function OnTriggerEnter(hit : Collider)
 {
     if(hit.gameObject == "Bulletspawn")
     {
         var exp = Instantiate(explosion, gameObject.transform.position, Quaternion.identity);
         Invoke( "ExplodeMe", lifeTime );
     }
 } 
 
 function ExplodeMe () {
     audio.PlayOneShot(explosionSound);
     Destroy(gameObject);
 }
Comment
Add comment · Show 2 · 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 Purpleshine84 · Apr 25, 2013 at 01:27 AM 0
Share

@alucardj Hey, thanks for all your help, and sorry for my late reply. And indeed there was some misunderstanding. Also sorry for that, and cheers ;)

avatar image YoungRichi · Apr 19, 2018 at 05:43 AM 0
Share

I used the invoke function and it worked. Thank you!

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

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

Deform mesh/terrain 0 Answers

Bullets Shot Stay Still 1 Answer

shot delay in between bullets 1 Answer

Collision with terrain 0 Answers

Bullets won't fire? 0 Answers


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