To make the enemy take damage from a specific projectile, you can add a check in the TakeDamage method to see if the projectile that hit the enemy is of the type you want to deal damage from. For example, if you want the enemy to take damage only from a bullet with the layer named “Bullet”, you can add a check like this:
public void TakeDamage(int damage,
GameObject projectile) {
if (projectile.layer == LayerMask.NameToLayer(“Bullet”)) {
health -= damage;
if (health <= 0) Invoke(nameof(DestroyEnemy), 0.5f);
} }
In this code, the TakeDamage method takes an additional argument for the projectile game object, and checks its layer. If the layer matches “Bullet”, the enemy’s health is reduced by the specified amount of damage.
To make the damage controllable, you can add a public field to the enemy script that allows you to set the damage value from the Unity editor. For example:
public class Enemy : MonoBehaviour {
public int damage = 10;
// …
public void TakeDamage(int damage, GameObject projectile) {
if (projectile.layer == LayerMask.NameToLayer("Bullet")) {
health -= damage;
if (health <= 0) Invoke(nameof(DestroyEnemy), 0.5f);
}
} }
Now, you can set the damage value for each enemy in the Unity editor, and the enemy will take that amount of damage when hit by a bullet.
I hope this helps! Let me know if you have any other questions.