• Unity
  • Services
  • Made with Unity
  • Learn
  • 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
  • Forums
  • Answers
  • Feedback
  • Issue Tracker
  • Blog
  • Evangelists
  • User Groups

Navigation

  • Home
  • Unity
  • Industries
  • Made with Unity
  • Learn
  • Community
    • Forums
    • Answers
    • Feedback
    • Issue Tracker
    • Blog
    • Evangelists
    • User Groups
  • Get Unity
  • Asset Store

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, shootableMask))
 
         {
 
             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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Side Scrolling Shooter. How to aim my bullet trajectory at the mouse cursor? 2 Answers

What is the best way to do third person melee combat? 1 Answer

I need help scripting a shooter 0 Answers

Third Person Controller 1 Answer

How do I instantiate and shoot a ball along a ray cast? [ANSWERED] 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges