- Home /
If the bullet is in proximity, then take damage?
So I am well into a game, I just need to add damage to the game. I want to have a script that does the following;
Check to see if the GameObject of bullet is 0.0 units away.
Subtract PistolDamage(33.0f) from EnemyHealth(100.0f)
When EnemyHealth is less than or equal to 0, destroy the gameobject the script is attatched to.
Answer by MT369MT · Sep 19, 2017 at 07:49 PM
Hi. Can't you use Collisions instead of check the distance? Here a solution with OnCollisionEnter:
public float EnemyHealt = 100.0f;
public float PistolDamage = 33.0f;
void Update()
{
if (EnemyHealt
{
Destroy(gameObject);
}
}
OnCollisionEnter(Collision Coll)
{
if (Coll.gameObject.tag == "Bullet")
{
EnemyHealt -= PistolDamage;
Destroy(Coll.gameObject);
}
}
Be sure to add the Tag Bullet to the Bullet prefab.
Answer by abdo400 · Sep 19, 2017 at 08:05 PM
float dist = Vector3.Distance(bullet.transform.position, target.transform.position);
if(dist <= 1) { target.health -= bullet.damage; }
if(target.health <= 0){ Destroy(target); }
Supposing that the enemy has a target.cs which includes a public int health and the bullet.cs includes a public int damage
Your answer
![](https://koobas.hobune.stream/wayback/20220612135051im_/https://answers.unity.com/themes/thub/images/avi.jpg)