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 Vel_1828 · Apr 18, 2014 at 10:52 AM · grenadeaoe

SIMPLIEST Area Damage Script (Java)

Good day everyone!

I'm bothering you with a fairly - I guess - easy problem, which I can not solve alone.

I'm a total beginner and trying to make a game SIMPLIEST AS POSSIBLE. A 2D shooter which has to be like Kalash - crudely simple and functional.

Lately I ran onto a problem. That is - a melee / grenade script. Both are supposed to deal the same amount of damage to all targets in the weapon range - a Trigger Cube. *(while melee is invisible, a grenade will spawn an explosion which I'll make later - that part is simple).

The Problem!

I'm working on tutorials for now and know some basics about Java. After I made the prototype script for the knife (invisible Trigger Cube in front of the character), it only "scans" for the first target in the Trigger Zone, the rest are unaffected.

Thy Script!

pragma strict

var TheDamage : int = 50; var WeaponSlotActive : GameObject; var AttackCooldown : int; var AttackInterval : float = 05; private var PoorBastard : GameObject;

function OnTriggerStay (other : Collider) {

 PoorBastard = other.gameObject;
 
 
 if (Input.GetButtonDown("Fire") && WeaponSlotActive.activeSelf == true && AttackCooldown == 0){
 
     AttackCooldown = 1;
     PoorBastard.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
     
     Debug.Log("SLASH!");
     yield WaitForSeconds (AttackInterval);
     AttackCooldown = 0;
     }
     
     }
         


Simple. When Knife is active, send damage on pressing "Fire" button to the Poor Bastard in the weapon range. Wrote it myself and with my Noob Status I'm bloody proud of it.

So, to solve the one-target problem, I googled it. Too bad, under all tags ("Unity aoe damage, splash damage, grenade, multi target trigger etc.) it finds only scripts, threads and discussions about things with no explanation, no sense or a million details with damage falloff, barriers between targets etc. - couldn't find the SIMPLE multi target script script.

The Question!

TL;DR

ill someone better oriented in Unity be brave enough to post (with a newbie explanation), what var and command should I add and where in the above code to make it multi-target?

What say you!?

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

Answer by Vel_1828 · Apr 18, 2014 at 06:57 PM

Well, that seems like a way out, but I want to stick to one-script solutions. Adding a script to every new NPC or destructible object would guarantee that I would forget to add it on some point. :P Also, if it was a "trap" that deals damage on trigger enter, then it's ok, but I want to deal damage when they are IN trigger AND when I press a button for knife attack. That complicates it. ;)

BUT! I won the life lottery and at last learned how to use the "Overlap sphere" option.

The script part with it sending damage to targets, for future generations:

PART 1:

ar MeleeRange = float; //This goes on the beginning of script, in vars var TheDamage = int; //Knoife Domoige. Derp.

Part 2:

//This goes to the bit where you deal damage. no aditional vars required:

     var PoorBastards = Physics.OverlapSphere(center, MeleeRange);
     
     for (var i = 0; i < PoorBastards.Length; i++) {
      if (PoorBastards[i].gameObject.tag == "Enemy"){
         PoorBastards[i].SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
     }}





DONE. :D Thanks for support!

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

Answer by KevLoughrey · Apr 18, 2014 at 01:44 PM

This isn't an answer to the question you're asking, but a different (and potentially simpler) way of solving your problem.

You don't necessarily need to send a message to the object being attacked. You could make a script for the enemy containing a variable for health and a simple function in that script which reduces their health whenever the knife trigger enters it. Then if you attach that script to each enemy they'll all be affected by the knife, and you won't have to worry about the knife scanning for each target in range.

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

21 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

Related Questions

OverlapSphere not causing damage? - Solved 2 Answers

Area of Effect damage trigger 2 Answers

SHOOT CORRECTLY DAMN GRENADE THING 2 Answers

GrenadeScript.cs Troubles 1 Answer

Physics Calculation: How to Calculate where Rigidbody will Drop ? 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