Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Kristov · Jun 03, 2011 at 10:19 AM · rigidbodyforcelayersaddexplosionforce

Applying explosion force to a single layer?

Hi all,

I have a feeling this is a really basic question, cause nobody else is asking it, but is there a way (using Rigidbody.AddExplosionForce) to apply an explosion force that will only effect a specific layer?

For context, I have an arcade game where when an enemy dies they break apart and I apply a small force in the center to make them blow apart nicely. However I see that the lasers that my player fires are sometimes affected by these and start spinning annoyingly.

Thanks for your help!

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
Best Answer

Answer by save · Jun 03, 2011 at 10:30 AM

Use tags instead (which is intended for the physics, layers are mainly intended for camera culling). It also helps if you post a little bit of code so we quickly can modify. Although here's an example of what you can do:

Firstly set a specific tag for the things you want to be affected by the explosion force.

Secondly add a statement of where you check what tag the object has, as an example here's a modified version of the Rigidbody.AddExplosionForce in the docs:

 var radius = 5.0;
 var power = 10.0;
 function Start () {
     // Applies an explosion force to all nearby rigidbodies
     var explosionPos : Vector3 = transform.position;
     var colliders : Collider[] = Physics.OverlapSphere (explosionPos, radius);
     
     for (var hit : Collider in colliders) {
 
         //Here the colliders with tag PhysAffected will be affected by the force
         if (!hit && hit.collider.tag=="PhysAffected")
             continue;
         
         if (hit.rigidbody)
             hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 3.0);
     }
 }

You could of course reverse this to check if the hit collider tag is the laser so it doesn't apply for that specific object.

Comment
Add comment · Show 3 · 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 Kristov · Jun 03, 2011 at 10:44 AM 0
Share

Excellent, thanks mate, that worked like a charm

avatar image kumarpratik · Feb 23, 2014 at 09:43 AM 0
Share

$$anonymous$$ake sure that this is for 3D. In 2D, things are different

avatar image Shinugami · Apr 20, 2014 at 09:40 AM 0
Share

This is great and all however regardless of how you filter the objects to which you will apply the 'force', one particular thing will not change.

AddExplosionForce(power, explosionPos, radius, 3.0);

This will apply force to any object with a rigidbody within the radius of the object you filtered out. What would be nice is if you can filter the 'AddExplosionForce' function so that it only affects a specific layer. It currently affects all rigidbodies.

avatar image
0

Answer by andrew-lukasik · Jan 31, 2013 at 06:05 PM

After doing some google-searches I found out that you need to write layer number like this:

 var colliders : Collider[] = Physics.OverlapSphere (transform.position, radius, (1 << layerMaskAsInt));

more here

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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

AddExplosionForce 0 Answers

What does rigidbodies sleeping have to do with collision detection? 0 Answers

Jump without character controller?? 2 Answers

Setting Max Angular Force For Y For Jumping 2 Answers

Rigidbody sphere slows down quickly 2 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