- Home /
Question by
jonathanbooker2911 · Dec 16, 2018 at 03:59 AM ·
scripting problemscript.scripting beginnerscriptingbasicsscript error
How do I make my projectile deal damage to a target it hit.
void Rocket()
{
currentAmmo--;
GameObject gameObject = Instantiate(projectile, transform) as GameObject;
Rigidbody rb = gameObject.GetComponent<Rigidbody>();
rb.velocity = transform.forward * projectileSpeed;
}
void Fire()
{
currentAmmo--;
GameObject gameObject = Instantiate(projectile, transform) as GameObject;
Rigidbody rb = gameObject.GetComponent<Rigidbody>();
rb.velocity = transform.forward * projectileSpeed;
}
I can't get my projectile to deal damage based on a varible in the script.
Comment
Best Answer
Answer by fuzzy_rick · Dec 16, 2018 at 04:12 AM
You will need a trigger collider on your projectile, then listen for OnCollisionEnter on the target. You can deal damage to your target there. Hope i understood the question.
GameObject gameObject = Instantiate(projectile, transform) as GameObject; Rigidbody rb = gameObject.GetComponent(); rb.velocity = transform.forward * projectileSpeed;
EnemyHealth enemyHealth = projectile.transform.GetComponent<EnemyHealth>();
if (enemyHealth != null)
{
enemyHealth.TakeDamage(damage);
}
Would this work trying to make a projectile deal damage based on infromation in my current script.