- Home /
Question by
LlamaNervous · Jul 08, 2014 at 09:29 AM ·
attackhphealth
Player Health Goes Down Too Fast/Too Much
I wrote a simple Enemy AI script where when the player is in range, it will do 20 damage to the player and the enemy would stop until his attack cool down was complete. However, when void DoDamage() takes place, it takes the 20 hp off more than once, in fact several times so that the hp will be -160.
Any help would be appreciated
Here are the relevant parts of my script:
public float attackRange = 15.0f;
public float attackDistance = 1f;
public float moveSpeed = 5.0f;
public float AttackCoolDown;
public bool Attacked;
public int EnemyDamage = 20;
PlayerHealth hpScript;
public GameObject player;
void Start ()
{
AttackCoolDown = 3f;
hpScript = player.GetComponent<PlayerHealth>();
}
void Update ()
{
if(Attacked)
{
AttackCoolDown -= Time.deltaTime;
}
if(AttackCoolDown < 0)
{
Attacked = false;
moveSpeed = 5f;
AttackCoolDown = 3f;
}
if (Distance < attackRange)
{
renderer.material.color = Color.red;
moveTo ();
}
if(Distance < attackDistance)
{
attack ();
}
}
void attack ()
{
if(AttackCoolDown >= 0)
{
renderer.material.color = Color.blue;
moveSpeed = 0;
Attacked = true;
DoDamage();
}
}
void DoDamage()
{
hpScript.hp -= 20;
PlayerPrefs.SetFloat("hp", hpScript.hp);
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613153517im_/https://answers.unity.com/themes/thub/images/avi.jpg)