• 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 COLLAnitySV · Dec 02, 2011 at 02:59 PM · colliderdamagesendmessagereceiverexplode

TNT Damage Receiver

Hi everyone, i've got a problem with my damage receiver script.

  1. How to make all players, AI, and objects got $$anonymous$$t / applied damage if they were in the TNT (TNT has explode all objects will affected by the explosion on certain range, i don't know how to explain (Damage between range))

  2. How to make if the player / bullets touched the TNT, the TNT Starts counting down (Beeping fast) till 3 seconds then boom.

I attached my scripts here thanks for the help! :D

 enum ExplosionTypes {TNT, Nitro, Other}
 var explosionType : ExplosionTypes;
 var explosion : Transform;
 var damage : float;
 var $$anonymous$$tPoints : float = 0;
 var deadReplacement : Rigidbody;
 var explosionFX : AudioClip;
 private var explodeSecs : float = 1;
 private var duration : float = 1;
 private var isTNT = false;
 
 function OnCollisionEnter (col : Collision){
     var contact : ContactPoint = col.contacts[0];
     var rot : Quaternion = Quaternion.FromToRotation(Vector3.up,contact.normal);
     var pos : Vector3 = contact.point;
     
     if(col.collider.gameObject.tag == "Player" || col.collider.gameObject.tag == "Bullets"){
         if(explosionType == ExplosionTypes.Nitro){
             Instantiate(explosion,pos,rot);
             Explode();
             audio.PlayOneShot(explosionFX);
             Destroy(gameObject);
             col.collider.SendMessage("ApplyDamage",damage,SendMessageOptions.DontRequireReceiver);
             }
         if(explosionType == ExplosionTypes.TNT){
             isTNT = true;
         }
         if(explosionType == ExplosionTypes.Other){
             Instantiate(explosion,pos,rot);
             Explode();
             audio.PlayOneShot(explosionFX);
             Destroy(gameObject);
             col.collider.SendMessage("ApplyDamage",damage,SendMessageOptions.DontRequireReceiver);
         }
         }
     
 }
 
 function Update () {
 var t : float = Mathf.PingPong (Time.time, duration) / duration;
     if(isTNT){
         renderer.material.color = Color.Lerp(Color.red,Color.gray,t);
     }
 }
 
 function Explode(){
     var expl : Transform;
     expl = Instantiate(explosion,transform.position, transform.rotation);
     Destroy(gameObject);
     if(isTNT) isTNT = false;
 }
 
 @script RequireComponent(AudioSource);
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by suruz@kento · Dec 02, 2011 at 04:59 PM

Solution to the first one is relatively easy. Just Modify Explode() so that it can determine its explotion effect rang. If am not wrong you are sending damage message to reciver using SendMessage("ApplyDamage",damage,SendMessageOptions.DontRequireReceiver). In that case using it with in Explode() will conform better reusability.

var radius: float = 100; // explotion radius

function Explode() {

 var expl : Transform;
 expl = Instantiate(explosion,transform.position, transform.rotation);
 Destroy(gameObject);
 if(isTNT) isTNT = false;

 var colliders : Collider[] = Physics.OverlapSphere (explosionPos, radius);
 for (var $$anonymous$$t in colliders) 
 {

    //you do'nt want to count TNT itself as $$anonymous$$t terget
 if($$anonymous$$t.name == name) return; 
  
     print("explotion $$anonymous$$t:" + $$anonymous$$t.name);
     $$anonymous$$t.SendMessage("ApplyDamage",damage,SendMessageOptions.DontRequireReceiver);
     //if you want to add explotion force
     var body: Rigidbody =$$anonymous$$t.gameObject.rigidbody;
     if(body)
     {
        body.AddForce(Vector3.up* 150);
     }
 
 }

}

When you use t$$anonymous$$s function you do'nt need to call SendMessage() and Destroy() after it.

You can use Invoke(handlerFunc, timeout) as a solution to the second one. you have timeout of 3 secs.

var BeepingSound: AudioClip;

var TimeOut: float = 3; //adjus as your requirement

function HandlerFunc() { audio.Stop(); Explode(); audio.PlayOneShot(explosionFX); }

function PlayBeeps() {

 audio.clip = BeepingSound; //fresh clip
 audio.Play();
 Invoke("HandlerFunc",TimeOut);
 

}

call PlayBeeps() in place of Explode() in function OnCollisionEnter ()

function OnCollisionEnter (col : Collision){ var contact : ContactPoint = col.contacts[0]; var rot : Quaternion = Quaternion.FromToRotation(Vector3.up,contact.normal); var pos : Vector3 = contact.point;

 if(col.collider.gameObject.tag == "Player" || col.collider.gameObject.tag == "Bullets"){
    if(explosionType == ExplosionTypes.Nitro){
      Instantiate(explosion,pos,rot);
      //Explode();
       PlayBeeps();
     }
    if(explosionType == ExplosionTypes.TNT){
      isTNT = true;
    }
    if(explosionType == ExplosionTypes.Other){
      Instantiate(explosion,pos,rot);
      //Explode();
       PlayBeeps() ;
     }
    }

}

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 COLLAnitySV · Dec 04, 2011 at 01:50 PM 0
Share

thanks it's work!! anyway what is explosionPos ? I use explosionPos : Vector3 = Vector3.up * 10;

avatar image suruz@kento · Dec 05, 2011 at 05:58 AM 0
Share

Sorry I missed that declaration. It will be var explosionPos : Vector3 = transform.position;

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.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do i Specify a Receiver for a Send Message Function? 2 Answers

SendMessage within a SendMessage Receiver 0 Answers

Damage receiver help! 1 Answer

Help with health reduction script 1 Answer

Ghost through enemy for a short duration after being damaged 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