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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by danieldoerksen · Dec 16, 2013 at 07:11 AM · javascriptexplosionradiusbombtimed

timed bomb, explosion detection

Im still quite new to coding in general, so I apologize for my improper indentation and etc. I learn somewhat from trial and error... but with something a little more complex like this im lost.. This script is planned to be used for multiple types of projectiles

so far i have the "on hit specific object hit" working...

but im stuck on if i set it up for timed explosion, how to get it detect nearby colliders and if "x" collider gets hit apply damage type of deal.

Heres what i got so far, my issues start at line 34 - 49

 var bulletLife : int;
 var bounces : int;
 var explodeOnHit : boolean = true;
 var explosionPrefab : Transform;
 private var creationTime = Time.time;
 var sound : AudioClip;
 var damage = 10;
 var areaOfEffectDamage : boolean = false;
 var explosionRadius : int;
 
 private var startTime;
 
 
 
 function Awake () {
 
 }
 
 function Update () {
 
 
     if (bounces==0){
         var explosion = Instantiate(explosionPrefab, transform.position, transform.rotation);
         Destroy(this.gameObject, 0); 
         AudioSource.PlayClipAtPoint(sound, transform.position);
         Debug.Log("boom");
         //AreaDamageEnemies();
     }    
         
     if (Time.time > (bulletLife+creationTime)) {
         Destroy(this.gameObject, 0);
         AudioSource.PlayClipAtPoint(sound, transform.position);
         var explosion2 = Instantiate(explosionPrefab, transform.position, Quaternion.Euler(Vector3(0, 0, 0)));
         Explode();
         }    
         
 function Explode () {
             
     GameObject.Find("Tank 01");
  
 var colliders : Collider[] = Physics.OverlapSphere (transform.position, radius);
  for (var hit : Collider in colliders) {
     if (Blocked(hit) != 0)
        continue;
  
  //where im gonna apply damage
  
     if (hit.rigidbody)
          Debug.Log("area effect boom");
 }
 }
     
 }
 
 
 
 //Basic collision detection checking for two differently named objects
 function OnCollisionEnter(theCollision : Collision){
 
 if (explodeOnHit==true && theCollision.gameObject.name == "Tank 01"){
         var explosion = Instantiate(explosionPrefab, transform.position, transform.rotation);
         Destroy(this.gameObject, 0); 
         AudioSource.PlayClipAtPoint(sound, transform.position);
         Debug.Log("tank hit");
 
     }
 
  if(theCollision.gameObject.name == "Cube"){
   Debug.Log("Hit the cube");
  }else if(theCollision.gameObject.name == "Plane"){
   Debug.Log("Hit the wall");
  }
  else if (theCollision.gameObject.name == "Tank 01") {
      theCollision.gameObject.SendMessage("OnDamage", damage);
  }
      bounces-=1;
 }
 
 function PlayAudio () {
     
 }
 

 
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by RetepTrun · Dec 16, 2013 at 01:37 PM

My grenades use

 function Start(){
   invoke("Explode",4)
 }
 
 function Explode(){
    explosion stuff..
 }
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 GameVortex · Dec 16, 2013 at 02:53 PM

With the OverlapSphere function you have already found all colliders within range of the explosion, and then you can just use the same message as you are using on line 74 to damage to the object, and also check the name as normally:

 function Explode () {
  
     GameObject.Find("Tank 01");
  
     var colliders : Collider[] = Physics.OverlapSphere (transform.position, radius);
     for (var hit : Collider in colliders) {
        if (Blocked(hit) != 0)
           continue;
  
        //where im gonna apply damage
        if(hit.name == "Tank 01") {
            hit.SendMessage("OnDamage", damage);
        }
 
  
        if (hit.rigidbody)
            Debug.Log("area effect boom");
     } 
 }
Comment
Add comment · Show 1 · 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 danieldoerksen · Dec 16, 2013 at 06:42 PM 0
Share

EDIT Turns out my gameobject needed a collider on the parent object

Okay, so that almost works.. i made a few syntax errors but i got that sorted.

the message "area effect objects" plays when in range of a cube with a rigidbody attached..

however its not detecting the "Tank 01" game object with rigidbody attached...

heres the updated code just the update() part

 function Update () {
 
 
     if (bounces==0){
         var explosion = Instantiate(explosionPrefab, transform.position, transform.rotation);
         Destroy(this.gameObject, 0); 
         AudioSource.PlayClipAtPoint(sound, transform.position);
         Debug.Log("boom");
         //AreaDamageEnemies();
     }    
         
     if (Time.time > (bulletLife+creationTime)) {
         Destroy(this.gameObject, 0);
         AudioSource.PlayClipAtPoint(sound, transform.position);
         var explosion2 = Instantiate(explosionPrefab, transform.position, Quaternion.Euler(Vector3(0, 0, 0)));
         AreaDamage();
         }    
         }
         
 function AreaDamage () {
             
  
   GameObject.Find("Tank 01"); 
  
 var colliders : Collider[] = Physics.OverlapSphere (transform.position, explosionRadius);
  for (var hit : Collider in colliders) {
     //if (Blocked(hit) != 0)
      //   continue;
  
     if (hit.name =="Tank 01"){
     hit.Send$$anonymous$$essage("OnDamage", damage);
          Debug.Log("area effect tank01 hit");
     }
 
 if (hit.rigidbody){
            Debug.Log("area effect objects");
     }
 }
 }
 

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

18 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

Related Questions

How do I destroy all enemy game objects with a bomb? 2 Answers

Help With Remote Explosive 1 Answer

OverlapSphere not causing damage? - Solved 2 Answers

I need one of my prefabs to be deleted after a few seconds 2 Answers

How to create a time bomb in Unity? 3 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