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 namekaw · Mar 26, 2013 at 11:15 PM · javascriptcollisionexplosion

Object only explodes when it collides with certain object, not all rigidbodies.

I have TNT that has an explosion script attached to it. I want it so that my TNT only explodes when it collides with my stockcar rather than other objects like the ground as well. I think I would usually know how to do this using tags but I used a script from a tutorial and am not sure how to put it in. I would also like to know how I would change the script so the sound plays only when the object collides with the stockcarHere is my entire script. Thanks,

var explosionRadius = 5.0;

var explosionPower = 10.0;

var explosionDamage = 100.0;

var explosionTime = 1.0;

var sound : AudioClip;

var soundVolume : float = 2.0;

function Start () {

 var explosionPosition = transform.position;
 var colliders : Collider[] = Physics.OverlapSphere (explosionPosition, explosionRadius);
 

for (var hit in colliders) {

     if (hit.gameObject.name == "Stockcar") {
         hit.rigidbody.AddExplosionForce(explosionPower, explosionPosition, explosionRadius, 3.0);
                  

         var closestPoint = hit.rigidbody.ClosestPointOnBounds(explosionPosition);
         var distance = Vector3.Distance(closestPoint, explosionPosition);

 

         // The hit points we apply fall decrease with distance from the hit point

         var hitPoints = 1.0 - Mathf.Clamp01(distance / explosionRadius);
         hitPoints *= explosionDamage;


         // Tell the rigidbody or any other script attached to the hit object 
         // how much damage is to be applied!
        hit.rigidbody.SendMessageUpwards("ApplyDamage", hitPoints, SendMessageOptions.DontRequireReceiver);

     }

 }


 if (sound){
 
 AudioSource.PlayClipAtPoint(sound, transform.position, soundVolume);

 // stop emitting ?

 if (particleEmitter) {
     particleEmitter.emit = true;
     yield WaitForSeconds(0.5);
     particleEmitter.emit = false;

 }

    }
    }
 
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 Benproductions1 · Mar 26, 2013 at 11:31 PM 0
Share

If you want the tnt to only explode when touching a specific object, you should probably post the code that causes the explosion, not the code that is the explosion :)

avatar image powerproc_chris · Apr 16, 2013 at 05:56 PM 0
Share

Is there a reason why you are using Physics.OverlapSphere ins$$anonymous$$d of just using a SphereCollider and OnTriggerEnter? I'm actively working on my own project with Unity and for this case I would say that the 2nd approach would do just fine.

avatar image namekaw · Apr 16, 2013 at 07:49 PM 0
Share

No reason, just used a script I found. I figured it out using another similar script and added as written below.

avatar image powerproc_chris · Apr 16, 2013 at 07:52 PM 0
Share

Ah I see, no problem. Good to hear you got it working!

2 Replies

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

Answer by namekaw · Apr 16, 2013 at 08:30 PM

Just needed this: if (collision.gameObject.name == "Stockcar")

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 robertbu · Mar 26, 2013 at 11:35 PM

You will need to run through the list first and check the tag or the name. Untested example code:

 var foundCar = false;
 
 for (var hit in colliders) {
     if (hit.name == "Car") {
         foundCar = true;
         break;
      }
 }
 if (!foundCar) return;

Replace "Car" with whatever you've name your car(s). Note you are currently only making the check once (in Start()). You need to either put it in Update(), or make it a function and call using InvokeRepeating(). Or make it a coroutine.

Comment
Add comment · Show 4 · 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 namekaw · Mar 27, 2013 at 12:30 PM 0
Share

I don't know why you would be breaking the code? It doesn't seem to work or make much sense to me. I tried hit.gameObject.name == "Car", but the TNT still explodes when it hits other objects.

avatar image robertbu · Mar 27, 2013 at 03:01 PM 0
Share

Post your modified code and let me take a look. "break" simply jumps out of the 'for' loop.

avatar image robertbu · Mar 27, 2013 at 04:01 PM 0
Share

@Benproductions1 is right. I assume this script was the explosive and made itself explode. But if there is code elsewhere that causes this to explode, then that is what we need to see.

avatar image namekaw · Apr 16, 2013 at 05:11 PM 0
Share

I have updated my script to show all of it. Now it actually does not explode when it hits anything including the car.

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

12 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

Related Questions

Adding sound javascript in unity2d 1 Answer

Object Collision PLEASE HELP!!! 1 Answer

How to add to a 3D object's length through code? 1 Answer

Trying to proceed to next level when multiple items have collided 1 Answer

My Javascript collision isn't being detected, please help. 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