- Home /
The question is answered, right answer was accepted
Damage by bullets issue. Solved.
Ok next problem.
I have two separate codes here that don't work exactly as wanted... again. One is the health of the AI and the other is the bullets being fired at this AI.
More specific is that the health is not reduced according to the damage that I have set in the bullet. It instead reduces the thing to 0 instantly when a bullet hits it.
Each bullet is supposed to do 10 damage and the health of the AI is 100. Even if I set the health 1000 it goes right down to zero and destroys the AI from one bullet.
Codes:
AI
public int maxHealth = 100;
public int curHealth = 100;
public void HealthAdjust(int damage){
curHealth =- damage;
if(curHealth < 0)
curHealth = 0;
if(curHealth > maxHealth)
curHealth = maxHealth;
//stop devision from zero.
if(maxHealth < 1)
maxHealth = 1;
if(curHealth == 0){
Destroy(gameobject);
}
}
//This is the bullet
void OnCollisionEnter(Collision col){
public int damage = 10;
//warhead is the effect.
GameObject.Instantiate(warhead, transform.position, Quaternion.identity);
col.gameObject.BroadCastMessage("HealthAdjust", damage, SendMessageOptions.DontRequireReceiver);
Destroy(gameObject);
}
So again, the bullet is not reducing the health by 10 as supposed to, instead it reduces all of the health no matter the actual size.
What am I doing wrong?
No... Woops I just typed in the same code here. They are separate.
Answer by meat5000 · Aug 14, 2013 at 11:07 AM
Try -= instead of =-
curHealth -= damage; //Take damage from curHealth and store result in curHealth;
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
make OnCollisionEnter2D ignore collisons of child objects 0 Answers
(C# and Java)Unable to give the Damage from enemy bullet script to Player Health. 3 Answers
Health UI Display 2 Answers