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 AMDAndrew · Oct 26, 2012 at 08:56 PM · coroutinedamageattackenemyai

Applying Damage

I am working on my AI Scipt for a while and I thought it's about time to make an attack feature. I am having a problem with the attack timing and amount of damage done. I have an ideea of what this could be caused by but I don't know how to solve it.

I am using Coroutine to call the function and yield to delay the damage. In the script below i have set to do -maxDamage at 1 seconds distance. The "health.curHealth -= maxDamage" is executed more than 20 times in one check and I don't know how to solve that?

 void Update(){
 .
 .
 .
 .
 if(health.curHealth > 1)
     attack = true;
     else if(health.curHealth < 1)
     attack = false;

 if ( attack == true ){
                 
                 StartCoroutine( Attack());
                                 
                 }
 .
 .
 .
 .
 .
 IEnumerator Attack () {
         animation.CrossFade(idleAnimation.name);
              while( health.curHealth > 1 ){
             yield return new WaitForSeconds(1.0f);
             Debug.Log(health.curHealth);
             Debug.Log(maxDamage);
             animation.CrossFade(attackAnimation.name);
             health.curHealth -= maxDamage;
             }    
     }

Debug.Log are showing the following : 100 - 50 50 - 50 0 - 50 0 - 50 0 - 50 (Repeating for 10 times or more)

Comment
Add comment · Show 4
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 Memige · Oct 26, 2012 at 09:10 PM 1
Share

Try putting a Debug.Log call right before health.curHealth = etc. and print out your curHealth and maxDamage variables to make sure they are what you expect them to be.

avatar image AMDAndrew · Oct 27, 2012 at 06:33 AM 0
Share

I have added as you said 2 debugs 1 for health and 1 for damage : 100 - 50 50 - 50 0 - 50 0 - 50 0 - 50

avatar image raoz · Oct 27, 2012 at 12:32 PM 1
Share

What function do you start the Attack() CoRoutine in? It might be that you are starting multiple instances of Attack() CoRoutine

avatar image AMDAndrew · Oct 27, 2012 at 02:31 PM 0
Share

Oh yes I frogot to post the funcion. It is Update function

5 Replies

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

Answer by raoz · Oct 27, 2012 at 03:10 PM

The problem is that Coroutine Attack() is started multiple times in Update(). Try this:

 private var gapTest = true;
 private var attacking = false;
 
 if(health.curHealth > 1)
     attack = true;
     else if(health.curHealth < 1)
     attack = false;
 
 if ( attack == true && !attacking){
 
           StartCoroutine( Attack());
           attacking = true;
 }
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 AMDAndrew · Oct 27, 2012 at 04:24 PM 0
Share

Ahh I would kiss you but sorry I'm a man

avatar image
1

Answer by Montraydavis · Oct 27, 2012 at 03:57 AM

Try the following:

IEnumerator Attack () {

animation.CrossFade(attackAnimation.name); yield WaitForSeconds(2.0f); // Change here health.curHealth -= maxDamage; attack = false ; // Change here

}

I don't know if you just didn't post it, but, I don't see you ever stopping the 'attack', and your yield is a bit off.

Comment
Add comment · Show 3 · 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 AMDAndrew · Oct 27, 2012 at 06:30 AM 0
Share

It doesn't work because attack variable is changed in Update depending of enemy's position compared to the player so it will become true right next frame and I will get the same result. Here is where it becomes false :

if( Chase == true && Distance > stopChasing ) { //2. $$anonymous$$ove towards target moveSpeed = change; controller.Simple$$anonymous$$ove(moveSpeed*transform.forward); attack = false; }

avatar image Montraydavis · Oct 27, 2012 at 07:37 AM 0
Share

Then why not just change WaitForSeconds ( 2.0f ) to WaitForSeconds ( TotalTimeHere ). Does that not work either ?

avatar image AMDAndrew · Oct 27, 2012 at 09:23 AM 0
Share

This works perfectly I have no problem with this, but the health.curHealth -= maxDamage is executed more than 10 times at once. How do I fix that ?

avatar image
1

Answer by Griffo · Oct 27, 2012 at 09:46 AM

Try this, alter the (0.05) to suit your requirements.

 private var gapTest = true;
 
 if(health.curHealth > 1)
     attack = true;
     else if(health.curHealth < 1)
     attack = false;
 
 if ( attack == true ){
 
           StartCoroutine( Attack());
 
           }
 .
 .
 .
 .
 .
 IEnumerator Attack () {
        animation.CrossFade(idleAnimation.name);
              while( health.curHealth > 1 ){
             yield return new WaitForSeconds(1.0f);
             Debug.Log(health.curHealth);
             Debug.Log(maxDamage);
             animation.CrossFade(attackAnimation.name);
 if(gapTest){
             health.curHealth -= maxDamage;
             GapSet();
                   }
             }  
     }
 
 function GapSet(){
 
     gapTest = false;
     yield WaitForSeconds (0.05);
     gapTest = true;
 
 }
Comment
Add comment · Show 3 · 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 AMDAndrew · Oct 27, 2012 at 12:21 PM 0
Share

Nope, the problem is still here. Expression "health.curHealth -= maxDamage" is executed like 40 times at once.

avatar image Montraydavis · Oct 27, 2012 at 02:53 PM 0
Share

Try putting the health before the yield, and see if that changes anything, though unlikely. $$anonymous$$ind posting the whole script ?

avatar image AMDAndrew · Oct 27, 2012 at 02:59 PM 0
Share

No, that won't change anything(in good) and sorry I wish I could post it but I've been working a lot on it and there will be others who will come and copy-paste it. Please understand

avatar image
0

Answer by skoandi · Oct 27, 2012 at 02:58 PM

I use something like this:

 function ApplyDamage(ammount : int){
 
    curHealth -= ammount;
 
 }
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 Montraydavis · Oct 27, 2012 at 05:01 PM

I totally understand. Maybe try adding an 'ease' function/routine of some sort? This is usually what I do until I am able to fully utilize .

var ApplyDamageCounter : float ;

var ApplyDamageCounterIndex : float ;

var ApplyDamageCounterMax : float = 1;

function ApplyDamage ( ammo : int )

{

if ( ApplyDamageCounter < ApplyDamageCounterMax )

{ curHealth -= ammount;

}else{

   ApplyDamageCounter += 1 * Time.deltaTime ;


}

}

This will allow only one damage query per second. You can manipulate this to your need, obviously.

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

14 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

Related Questions

Enemy attack constantly with a coroutine 1 Answer

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

Enemy will only take (melee) damage when the distance is below 1 (ie. A zero with decimals after it) 2 Answers

Need help developing Damage Over Time mechanic 0 Answers

Melee Damage script by collision 2 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