• 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 Punkjim420 · Aug 23, 2012 at 08:21 PM · healthdamageenemy ai

Enemy health drops but not if first enemy lives, how do i change that?

My script works perfectly except, if enemy1 hasnt been "killed"(when health = 0, it dies.) enemy2 cant be attacked. I need to be able to reduce any enemies health when pressing the Attack button and enemy is inside the attackThreshold..

like i said this all works, just i cant kill enemy2 unless enemy1 is dead. Anyone have any idea what i can do to achieve this?

using UnityScript by the way.

This is my EnemyScript.js

 private var enemyMaxHp : float;
 private var enemyCurHp : float;
 var startingHealth : float = 100.0;
 var target : Transform;
 var moveSpeed = 10;
 var rotationSpeed = 10;
 var attackThreshold = 10; //distance in which to attack
 var giveUpThreshold = 51; //how close you get before enemy start chase
 var attackRepeatTime = 100; //attack delay
 private var damage;
 private var chasing = false;
 var attackTime;
 var myTransform : Transform; //current transform data of the enemy
 private var distance;
 var idle;
 myTransform = transform; //cache transform data for easy access/performance
 
 function Awake(){
 attackTime = Time.time;
 myTransform = transform; //cache transform data for easy access/performance
 }
 
 function Start(){
  enemyMaxHp = startingHealth;
  enemyCurHp = startingHealth;
     target = GameObject.FindWithTag("Player").transform; //target the player
 }
 
 
 function Update () {
 distance = (target.position - myTransform.position).magnitude;
     if (enemyCurHp > enemyMaxHp){
         enemyCurHp = enemyMaxHp;
     }
     if (enemyCurHp < 0){
         enemyCurHp = 0;
     }
     
     if (enemyCurHp < 1){
     Destroy(gameObject);
     }
             
         if(chasing){
             Chase();
         }
         
         if(distance > giveUpThreshold) { //give up if too far away
             chasing = false;
             idle = true;
             }
         
         if(idle){
             //Idle animation.
         }
         //not currently chasing
         //start chasing if target comes close
         if(distance < giveUpThreshold) {
         chasing = true;
         }
         
         if(distance < attackThreshold){
         Attack();
         }
 }
 
 function ApplyEnemyDamage (enemyDamage : float) {
 distance = (target.position - myTransform.position).magnitude;
     if(distance < attackThreshold){
     enemyCurHp = enemyCurHp - enemyDamage;
     Debug.Log(enemyCurHp);
     }
 }
 
 function Attack () {
 chasing = false;
     if(attackTime < Time.time){
     // Calls the function ApplyDamage with a value of 5
     gameObject.FindWithTag("Player").SendMessage ("ApplyDamage", 5.0);
     attackTime = Time.time + attackRepeatTime;
     }
 }
 function Chase(){
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime; //move to player
 }

 

this is the attack inside of my PlayerScript.js

 function PlayerAttack () {
 chasing = false;
     if(attackTime < Time.time){
     // Calls the function ApplyDamage with a value of 5
     gameObject.FindWithTag("Enemy").SendMessage ("ApplyEnemyDamage", 10.0);
     attackTime = Time.time + attackRepeatTime;
     }
 }
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 BLarentis · Aug 23, 2012 at 09:31 PM

Hello, I believe that the problems is here:

 gameObject.FindWithTag("Enemy").SendMessage ("ApplyEnemyDamage", 10.0);

You are looking for a Game Object with the tag "Enemy". Since you have more than one, you will have an array of Game Objects with this tag, since Enemy 1 its the first in line, you will always send the damage to him. When he dies, he is deleted, and then this find will return the second enemy. Thats why you need to kill the first one, so this find will find the second enemy.

What you will have to do, its not use the FindWithTag, you will have to make the thing that causes damage to the enemy send the message to the enemy that he is colliding. If you are shooting at them, you will have the bullet to cause damage to the one that took the shot, if its a sword, you will have the sword send the attack to the enemy. Take care!

Comment
Add comment · Show 1 · 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 Punkjim420 · Aug 24, 2012 at 02:16 PM 0
Share

thanks, that was exactly it. :)

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

One enemy triggers all the enemies 2 Answers

rigidbody enemy goes only forward, no damage delay. 2 Answers

how to make enemy damage player? 1 Answer

how do i make a script that calculate how much damage did i loss 1 Answer

For loop on collision (two hit kill) 2 Answers

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