• 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 Wiremuch · Apr 15, 2015 at 01:07 PM · explosionplaymakergrenade

2D explosion that targets specific gameobjects

Hey,

I'm making a 2D platformer and wanted to ask what is the best way to create a grenade like explosion? Basically a 2D circular area where anyt$$anonymous$$ng with a specific tag that is wit$$anonymous$$n the circular area is destroyed. I've been using playmaker for most of the game and to be honest not entirely sure how to solve t$$anonymous$$s via that.

In essence what I'm after is a way that any game object with the tag 'enemy' w$$anonymous$$ch are wit$$anonymous$$n an area has a message sent to it to do somet$$anonymous$$ng, instead of everyt$$anonymous$$ng with the tag 'enemy' in the level being told to do somet$$anonymous$$ng (w$$anonymous$$ch I did manage through arraymaker).

Note: I'm relatively new to programming in Unity.

Thanks!

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

Answer by _joe_ · Apr 15, 2015 at 01:32 PM

I don't know how to make it via Playmaker, but in C# it's pretty simple. Create an overlap sphere at the point of explosion, and gather all the colliders of tag "anytagthatyouwant" in an array, And then simply loop that array and do whatever you want with them (like adding an ExplosionForce).

A link to Explosion Force, http://docs.unity3d.com/ScriptReference/Rigidbody.AddExplosionForce.html

And a small change to the script for you, Here you go:

 void Start() {
         Vector3 explosionPos = transform.position;
         Collider[] colliders = Physics.OverlapSphere(explosionPos, radius);
         foreach (Collider $$anonymous$$t in colliders) {
             if ($$anonymous$$t.gameObject.tag =="yourtag" && $$anonymous$$t.rigidbody)
                 $$anonymous$$t.rigidbody.AddExplosionForce(power, explosionPos, radius, 3.0F);
             
         }
Comment
Add comment · Show 7 · 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 Wiremuch · Apr 15, 2015 at 02:11 PM 0
Share

That's exactly what I was after! Thanks Joe!

avatar image zero_null · Aug 12, 2016 at 01:01 PM 1
Share

but it's not 2d!

avatar image bilsisco · Sep 28, 2020 at 10:53 AM 0
Share

not 2D this script

avatar image ronliew13 bilsisco · Oct 26, 2020 at 11:20 AM 1
Share

Hey not sure if you still need this, but for whoever wants the 2D version of this code use Physics2D:

 void Start() {
          Vector3 explosionPos = transform.position;
          Collider2D[] colliders = Physics2D.OverlapCircleAll(explosionPos, radius);
          }

The Physics2D.OverlapCircleAll method returns a list of of all colliders in the circle. Hope this helps!

https://docs.unity3d.com/ScriptReference/Physics2D.OverlapCircleAll.html

avatar image Finbox_Entertainment ronliew13 · Nov 06, 2022 at 06:10 AM 0
Share

And thank you, good sir!

Show more comments
avatar image logicandchaos · Nov 06, 2022 at 05:10 PM 0
Share

you could also avoid tags altogether if you set up the collision matrix, then it will if some objects have different tags.

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

8 People are following this question.

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

Unity3D grenade not exploding correctly 1 Answer

Make grenade roll out when it leaves hand 1 Answer

Explosion Damage not being applied. 1 Answer

Make grenade apply force to rigidbodies around it 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