using UnityEngine;

public class ProjectileShoot : MonoBehaviour {
    public bool automatic = false;
    public GameObject bullet;

    public Camera camera;

    public float damage = 10f;
    public float fireRate = 2f;


    private float nextTimeToFire;
    public ParticleSystem particle;
    public float range = 100f;

    private void Update() {
        if (automatic) {
            if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire) shoot();
        }
        else {
            if (Input.GetButtonDown("Fire1") && Time.time >= nextTimeToFire) shoot();
        }
    }

    protected virtual void shoot() {
        particle.Play();
        GameObject clone = Instantiate(bullet, transform.position, Quaternion.LookRotation(-camera.transform.forward) );
        clone.transform.forward = camera.transform.forward;
        nextTimeToFire = Time.time + 1f / fireRate;
    }
}