I have a basic missile launcher set up. The collision works fine, the explosion animation plays when the missile collides with something, and then it gets destroyed. The only problem is, I can’t shoot any more missiles after the first one!
I have a prefab named “missile” with a rigidbody attached, which is associated with the projectile variable from MissileLauncher. Is there another way to instantiate a new projectile after destroying the previous gameObject?
public class MissileLauncher : MonoBehaviour
{
public Rigidbody projectile;
int speed = 20;
void Update ()
{
if (Input.GetButtonDown("Fire1"))
{
projectile = (Rigidbody)Instantiate(projectile, transform.position, transform.rotation); //Instantiate projectile
projectile.velocity = transform.TransformDirection(new Vector3(0,0,speed)); //Set projectile velocity
Physics.IgnoreCollision(projectile.collider, transform.root.collider); //Avoid projectile an hero
}
}
}
public class Projectile : MonoBehaviour
{
public GameObject explosion;
void OnCollisionEnter(Collision collision)
{
ContactPoint contact = collision.contacts[0];
transform.rotation = Quaternion.FromToRotation (Vector3.up, contact.normal);
explosion = (GameObject)Instantiate(explosion, contact.point, transform.rotation);
Destroy(gameObject);
}
}
public class Explosion : MonoBehaviour
{
float explosionTime = 0.25f;
void Start ()
{
Destroy (gameObject, explosionTime);
}
}