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 Taty · Oct 28, 2014 at 05:13 AM · shooteraoe

Make a damage in area from a shoot.

Hey there everyone.

I've got the last Unite 2014 tutorial, where you make a simple shooter with bunnies. I'm trying to make that the shoot, when hit an enemy, it does massive damage around, in a sphere area, but I can't seem to figure it out.

I have this function on player:

 void Shoot ()
     {
         timer = 0f;
 
         gunAudio.Play ();
 
         gunLight.enabled = true;
 
         gunParticles.Stop ();
         gunParticles.Play ();
 
         gunLine.enabled = true;
         gunLine.SetPosition (0, transform.position);
 
         shootRay.origin = transform.position;
         shootRay.direction = transform.forward;
 
         if(Physics.Raycast (shootRay, out shootHit, range, shootableMask))
         {
             EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> ();
             if(enemyHealth != null)
             {
                 enemyHealth.TakeDamage (damagePerShot, shootHit.point);
             }
             gunLine.SetPosition (1, shootHit.point);
         }
         else
         {
             gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range);
         }
     }

And in enemy, he receives damage with:

 public void TakeDamage (int amount, Vector3 hitPoint)
     {
         if(isDead)
             return;
         
         enemyAudio.Play ();
 
         currentHealth -= amount;
         
         hitParticles.transform.position = hitPoint;
         hitParticles.Play();
         
         if(currentHealth <= 0)
         {
             Death ();
         }
     }

Can anyone help me with that?

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

Answer by AlwaysSunny · Oct 28, 2014 at 05:09 AM

Have a look at Physics.OverlapSphere(), that's probably what you'll wanna use.

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 Taty · Oct 28, 2014 at 05:17 AM 0
Share

Umh.. I may be going in the wrong way.

I tried

 if (Physics.CheckSphere(shootHit.point, sphereRadius))
                 {
                 enemyHealth.TakeDamage (damagePerShot, shootHit.point);
                 }

Sorry, did say something wrong. Actually it just don't make an area of range effect. It has nothing to do with enemies in a circle, that is another thing I've done.

avatar image AlwaysSunny · Oct 28, 2014 at 05:22 AM 0
Share

Physics.CheckSphere will return a list of colliders that touch the sphere described by the arguments. You then check each collider for an Enemy component, and only try calling TakeDamage on found components.

avatar image Taty · Oct 28, 2014 at 05:32 AM 0
Share

Right, this is a little bit complex for me. I got the main idea. This code will check if there are enemies around the sphere. I need to select witch colliders are enemies and then apply TakeDamage on each collider that fits the description.

I just can't figure it out how to translate this to a code. :(

avatar image AlwaysSunny · Oct 28, 2014 at 07:09 AM 0
Share

Sorry, I was mistaken earlier. Edited my answer. You want Physics.OverlapSphere(), which returns an array of colliders. iterate over the array, checking each collider for the desired componenet. If it's not null, call TakeDamage() on it.

http://docs.unity3d.com/ScriptReference/Physics.OverlapSphere.html

avatar image Taty · Oct 28, 2014 at 05:34 PM 0
Share

Just tried and worked! :)

Thank you so much!

In case anyone want the code as referece, here it is:

 if(Physics.Raycast (shootRay, out shootHit, range, shootable$$anonymous$$ask))
 
         {
 
             Collider[] hitColliders = Physics.OverlapSphere(shootHit.point, radius);
             int i = 0;
 
             while (i < hitColliders.Length) {
                 
 
                 if (hitColliders[i].tag == "Zombunny" || hitColliders[i].tag == "Zombear" || hitColliders[i].tag == "Hellephant")
                 {
                     Debug.Log("Hit: " + hitColliders[i].name);
 
                     EnemyHealth enemyHealth1 = hitColliders[i].GetComponent<EnemyHealth>();
                     if (enemyHealth1 != null)
                     {
                         enemyHealth1.TakeDamage(damagePerShot, shootHit.point);
                     }
                 }
 
                 i++;
             }
 
 
 
             EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> ();
             if(enemyHealth != null)
             {
                 
                             enemyHealth.TakeDamage (damagePerShot, shootHit.point);
 
                                                 
             }
             gunLine.SetPosition (1, shootHit.point);
         }
         else
         {
             gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range);
         }

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Sidescroll Shooter Crosshair? 0 Answers

Weapon pick up 3 Answers

Gun Wont Follow Camera Up and Down With Unity's First Person Controller 2 Answers

Altering Material Alpha While Raycast is NOT Hitting 0 Answers

Awareness script doesnt work 0 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