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 /
  • Help Room /
avatar image
0
Question by Macchus · Mar 09, 2017 at 03:01 PM · prefabstriggersenemy spawnenemydamage

How can I make my melee attack hit multiple prefab enemies?

I have my game set up and it seems to work fine when I melee combat the enemy I created. It's takes 3 hits and dies like I would expect.

I can't seem to damage the prefabs that I spawn on the level.

I can duplicate the Enemy gameObject, but then only the most recent duplicate will take damage and be affected by animation controllers.

I think it has something to do with my AttackTrigger c# script not targeting multiple enemies?

Here are my scripts

"Player Attack Script:"

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityStandardAssets.CrossPlatformInput;
 
 public class PlayerAttack : MonoBehaviour {
 
     private bool attacking = false;
 
     private float attackTimer = 0;
     private float attackCooldown = 0.3f;
 
     public Collider attackTrigger;
 
     private Animator anim;
 
     void Awake (){
         anim = gameObject.GetComponent<Animator> ();
         attackTrigger.enabled = false;
     }
 
     void Update (){
         Attack ();
     }
  
     void Attack () {
         bool isAttacking = CrossPlatformInputManager.GetButton ("Attack");
 
         if ((isAttacking) && !attacking)
         {
             attacking = true;
             attackTimer = attackCooldown;
             attackTrigger.enabled = true;
         }
 
         if (attacking)
         {
             if (attackTimer > 0) 
             {
                 attackTimer -= Time.deltaTime;
             }
             else
             {
                 attacking = false;
                 attackTrigger.enabled = false;
             }
         }
         anim.SetBool("isAttacking", attacking);
     }
 }

"Player Attack Trigger Script:"

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class AttackTrigger : MonoBehaviour {
 
     public int attackDamage = 10;   
                       
     GameObject enemy;
     GameObject enemies;
     PlayerHealth playerHealth;                  // Reference to the player's health.
     EnemyHealth enemyHealth;                    // Reference to this enemy's health.
     bool enemyInRange;                         // Whether player is within the trigger collider and can be attacked.
     float timer;                                // Timer for counting up to the next attack.
 
 
 
     void Awake ()
     {
         enemy = GameObject.FindGameObjectWithTag ("Enemy");
 
         enemies = GameObject.FindGameObjectWithTag ("Enemy");
 
         enemyHealth = enemy.GetComponent<EnemyHealth> ();
     }
 
     void OnTriggerEnter (Collider other)
     {
         if(other.gameObject == enemy)
         {
             //Debug.Log ("I Touched " + other.name);
 
             enemyHealth.TakeDamage (attackDamage);
 
             if(enemyHealth.currentHealth > 0)
             {
                 enemyHealth.TakeDamage (attackDamage);
             }
         }                
     }    
 }  
 

"EnemyHealth Script:"

 using UnityEngine;
 
 public class EnemyHealth : MonoBehaviour
 {
     public int startingHealth = 30;    
     public int currentHealth;         
     public int scoreValue = 10; 
 
     bool damaged;                                               // True when the player gets damaged.
     bool isDead;   
     Animator anim;    
 
     private float timer = 0;
     private float timerCooldown = 0.2f;
 
     void Awake ()
     {
         anim = GetComponent <Animator> ();
         bool isDead = false;
         currentHealth = startingHealth;
     }
 
     void Update ()
     {            
         timer += Time.deltaTime;
 
         if (damaged)
         {
             anim.SetBool ("isHit", true);
         //    timer = 0;
         }
 
         if (timer >= timerCooldown)
         {
             anim.SetBool ("isHit", false);
         }
     }
 
 
     public void TakeDamage (int amount)
     {
         damaged = true;
 
         currentHealth -= amount;
 
         if(currentHealth <= 0 && !isDead)
         {
             Death ();
         }
             
         timer = 0;
     }
 
     void Death ()
     {
         isDead = true;
         ScoreManager.score += scoreValue;
         Destroy(gameObject);
     }
 }

Any help on why I can only attack the original and not any instaniated (clone) prefabs of the same enemy.

I can have multiple prefabs hit me.. but I can't see to hit or dmg more than just the one original enemy.

Thanks alot 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by hexagonius · Mar 09, 2017 at 06:00 PM

within your player attack trigger you're saving references to just one enemy and in on trigger enter you compare the result with it.
instead, just compare the on trigger result with the enemy tag, which will get you every enemy. if that succeeds run GetComponent on it do get the Health script and deal damage.

Comment
Add comment · Show 2 · 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 Macchus · Mar 09, 2017 at 06:26 PM 0
Share

After reading your comment, I think this could be a possible fix? I'll have to test when I'm home ofc.

   void OnTriggerEnter (Collider other)
      {
          if(other.gameObject == enemy)
          {
              enemyHealth = other.GetComponent<EnemyHealth>();
  
              if (enemyHealth.currentHealth > 0)
              {
                  enemyHealth.TakeDamage (attackDamage);
              }
          }                
      }    

Thoughts?

Regardless thank you for your help pointing me in the right direction :)

avatar image hexagonius Macchus · Mar 09, 2017 at 06:27 PM 0
Share
 if(other.gameObject.CompareTag("Enemy"))

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

98 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

Related Questions

Turn on Prefab with Keycode issue 2 Answers

Communicate between scenes with prefabs? 3 Answers

How to assign transforms to a Prefab?,How can I assign transforms to Prefabs? 1 Answer

Why is OnTriggerExit not firing? 3 Answers

Limit OnTriggerEnter to work with only specific Child Object Colliders 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