Question by
n-carlson1 · Jul 17, 2016 at 03:42 PM ·
collisionrigidbodyinstantiatecollision detectionprojectile
Is there any good sample code for shooting a projectile and collision detection?
I've been trying to figure out how write good code that includes shooting a projectile and when it hits the enemy it sends a collision message that can be used to apply damage based on the projectile. What I have currently is not very good at all.
on the player
GameObject shot = GameObject.Instantiate(attackProjectile, transform.position + (transform.forward) + (transform.up), transform.rotation) as GameObject;
shot.GetComponent<Rigidbody>().AddForce(transform.forward * projectileSpeed);
and on the projectile
void OnCollisionEnter(Collision c)
{
//destroy projectile when it collides with anything
Destroy(gameObject);
enemyHealth.TakeDamage(damage);
}
I couldn't figure out how to do it so I watched a tutorial and this is what I got basically, a very inefficient way to do it. So I was hoping for maybe a guide to a better way to do it.
Comment