• 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
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 anything with a specific tag that is within 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 this via that.

In essence what I'm after is a way that any game object with the tag 'enemy' which are within an area has a message sent to it to do something, instead of everything with the tag 'enemy' in the level being told to do something (which I did manage through arraymaker).

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

Thanks!

Comment

People who like this

0 Show 0
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
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 hit in colliders) {
             if (hit.gameObject.tag =="yourtag" && hit.rigidbody)
                 hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 3.0F);
             
         }
Comment
logicandchaos

People who like this

1 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

3rd Person Throwing Grenade Question 0 Answers

OverlapSphere not causing damage? - Solved 2 Answers

Raycast collider detector lags horribly? 2 Answers

Null reference and GetComponent 1 Answer

How do I destroy all enemy game objects with a bomb? 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