Best practice where to store Projectile Damage amounts?
I have a projectile and an enemy, both with colliders. On the projectile script, I'm destroying the projectile OnTriggerEnter:
public class bulletBehavior : MonoBehaviour
{
private Rigidbody rb;
public float smallGunSpeed = 500f;
void Start()
{
rb = this.GetComponent<Rigidbody>();
rb.AddForce(transform.forward * smallGunSpeed);
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Enemy")
{
Destroy(this.gameObject);
}
}
}
On the Enemy Script I'm handling the health and the damage from the different projectiles using tags. Is this the right way to do it? I would much rather attach the damage amount to the projectile but I'm not sure how to "Transfer" the damage value to the NPC script from the projectile script? Storing them on the NPC script seems incredibly inefficient.
public class NPCscript : MonoBehaviour
{
public int maxHealth = 300;
public int currentHealth;
public HealthBar healthBar;
private GameObject enemy;
void Start()
{
currentHealth = maxHealth;
healthBar.SetMaxHealth(maxHealth);
StartCoroutine(AutoHeal());
}
void Update()
{
if (currentHealth <= 0)
{
Destroy(this.gameObject);
}
}
void TakeDamage(int damage)
{
currentHealth -= damage;
healthBar.SetHealth(currentHealth);
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Bullet")
{
TakeDamage(10);
}
else if (other.tag == "Grenade")
{
TakeDamage(75);
}
}
}
Answer by RealFolk · Jan 26, 2021 at 03:03 PM
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Enemy")
{
Other.GetComponent<NPCscript>().currentHealth -= 1;
Destroy(this.gameObject);
}
}
Your answer
Follow this Question
Related Questions
Inconsistent 2d collider hit registration 3 Answers
Client can damage host but not vice versa? 0 Answers
How to do multiple inheritance (or workaround) with Unity? 1 Answer
How to deal damage to only enemy hit? 2 Answers
Sound on collision. 1 Answer