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 Babilinski · Nov 02, 2011 at 10:16 PM · distancerangeenemies

enemies in my Range

I am creating a combat script and i need some help.

I know that

 float distance = Vector3.Distance(target.transform.position, transform.position);

gets the Distance between me and the target that i have selected.

now, how can I make all the object in that range my target. So, that I can get the script that they cary and change there health.

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

Answer by aldonaletto · Nov 03, 2011 at 12:42 AM

You could use Physics.OverlapSphere: it returns an array of all colliders that intersect an imaginary sphere of a given radius. You can then check the tags to know which ones are enemies, and apply the damage:

    var cols: Collider[] = Physics.OverlapSphere(transform.position, radius);
    for (var col: Collider in cols){
        if (col && col.tag == "Enemy"){ // if object has the right tag...
            var script: EnemyScript; // assuming the enemy script is called EnemyScript
            script = col.GetComponent(EnemyScript);
            script.health -= 5; // apply damage 5
        }
    }
Or, in C#:

    Collider[] cols = Physics.OverlapSphere(transform.position, radius);
    foreach (Collider col in cols){
        if (col && col.tag == "Enemy"){ // if object has the right tag...
            // assuming the enemy script is called EnemyScript
            EnemyScript script = col.GetComponent<EnemyScript>();
            script.health -= 5; // apply damage 5
        }
    }
Comment
Add comment · Show 8 · 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 Babilinski · Nov 03, 2011 at 12:48 AM 0
Share

Do you have it in C sharp? I understand it better that way.

avatar image aldonaletto · Nov 03, 2011 at 12:50 AM 0
Share

I posted a C# version too. I'm a JS guy, thus this code may have awful C# errors!

avatar image Babilinski · Nov 03, 2011 at 12:53 AM 0
Share

Thank you so much. I'll sing at your wedding

avatar image syclamoth · Nov 03, 2011 at 01:01 AM 0
Share

Nitpicking- there's no point having the

 if(col && col.tag == whatever)

thing all in one line- if 'col' returns null, 'col.tag' will always throw a null-reference-exception!

Other than that, your code is fine.

avatar image aldonaletto · Nov 03, 2011 at 02:27 AM 0
Share

If col is null, the clause is false and the second condition is not tested - or at least it should not be; this was one of the great new ideas brought to the program$$anonymous$$g world by the C language. I suppose (and hope) that Unityscript and C# follow this rule.
Anyway, I suspect that col will never be null, but I saw this test in the AddExplosionForce example and decided not to risk.

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Detect if player is in range? 2 Answers

Gravity within range 1 Answer

How to use Sphere colliders to detect the "Player" 1 Answer

GetMouseButtonDown From a close distant 1 Answer

How To Properly Return An Object If Within Distance Range? 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