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 Consul-Livius · Dec 27, 2017 at 07:15 PM · gameobjectdestroyoverlapsphereradius

How to Destroy gameObjects and deal damage within a radius except player ?

Hello,

Ive been wondering how to I achieve a destruction radius that also does damage to other gameobjects except my player. In short my player is launching a missile, after a few secconds that missile explodes , and when it explodes i want it to destroy all gameobjects within a radius or deal damage to them except the player.

I`ve been looking at the Physics.OverlapSphere but i cant get my way to work it.

thanks guys.

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
2

Answer by Taylor-Libonati · Dec 27, 2017 at 08:15 PM

Physics.OverlapSphere is a good way to go. You will be doing almost exactly what the example script is doing although I would suggest that instead of using SendMessage you call an actual function. Example script

So I will break down the general idea. You get a set of colliders that are in range, then you loop through them and if it is an object that you can hit you apply the damage or just destroy them.

This gives you a list of colliders

 Collider[] hitColliders = Physics.OverlapSphere(center, radius);


This loops through them

 for(int i=0; i<hitColliders.Length; i++){

This checks if it is an object you want (using tags, but you could do this other ways)

 if(hitColliders[i].gameObject.tag == "Enemy"){


This calls a function on the object

 EnemyBaseClass enemy = (EnemyBaseClass)hitColliders[i];
 if(enemy != null){
      enemy.TakeDamage();
 }


If you want more specific help then that you will have to post your code so we can see where you are going wrong.

Comment
Add comment · Show 5 · 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 Consul-Livius · Dec 27, 2017 at 10:38 PM 0
Share

thanks my friend but i get an error athe the for loop " cs0019 '<' cannot be applied to operands of type int and unityEngine.Collider[]

avatar image Taylor-Libonati Consul-Livius · Dec 28, 2017 at 12:07 AM 0
Share

That's probably just a syntax error. I didn't test my code so there might be little things you need to change. If you post your code I or other people will be able to help you better. Also there are a lot of Unity tutorials that $$anonymous$$ch you how to do things like missiles and grenades if you want to read up on the subject.

avatar image Consul-Livius Taylor-Libonati · Dec 31, 2017 at 02:55 PM 0
Share

Sorry for the late reply but here is my rocket code:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class RocketPlayer : $$anonymous$$onoBehaviour {

 public Rigidbody rocketRb;

 public GameObject explode;

 public float radius = 10f;
 public float speed;
 public float life = 2;

 private Vector3 center;

 public bool isDeadRocket;

 void Start () 
 {
     isDeadRocket = false;
     rocketRb.velocity = transform.forward * speed;


 }
 

 void Update () 
 {
     StartCoroutine (RocketDeath ());

 }

 IEnumerator RocketDeath ()
 {
     yield return new WaitForSeconds (life);
     Destroy (gameObject);
     Instantiate (explode, transform.position, transform.rotation);

 }

     void Explode ()
     {
         center = transform.position;
         Collider[] hitColliders = Physics.OverlapSphere (center, radius);
         for (int i = 0; i < hitColliders; i++) 
         {
             if (hitColliders[i].gameObject.tag == "Cube") {
                 Destroy (gameObject.tag == "Cube");
             }
         }
     }

}

Show more comments
avatar image
0

Answer by Consul-Livius · Jan 09, 2018 at 10:25 AM

I resolved it on my own, i just instantiated an empty game object with a sphere colliders that does the job upon rocket death.

Thanks for the help!

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

107 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

destroy gameobjects within overlapsphere 2 Answers

How to Destroy Game Object In All Clients in Photon Network 0 Answers

Destroy all GameObjects EXCEPT some... 2 Answers

Why won't my script load? 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