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)
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.
Griffo
October 27, 2012, 9:46am
3
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;
}
raoz
October 27, 2012, 3:10pm
5
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;
}
I use something like this:
function ApplyDamage(ammount : int){
curHealth -= ammount;
}
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.