using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Gun : MonoBehaviour {

    public Transform bullet;
    public Transform tip;
    public float bulletSpeed = 1000;
  

    private bool shotAvailable;
    private Transform clone;
    private Rigidbody rb;
    private bool input;
	// Use this for initialization
	void Start () {

        shotAvailable = true;

	}

    // Update is called once per frame
    void Update()
    {
        if (SceneManager.GetActiveScene().name == "scene3")
        {
            input = false;
        }
        else if (Input.GetButtonDown("Fire1"))
            input = true;
        if (input && shotAvailable)
        {

            shotAvailable = false;

            clone = Instantiate(bullet, tip.transform.position, tip.transform.rotation);
            GetComponent<AudioSource>().Play();

            rb = clone.GetComponent<Rigidbody>();
        }
    }

    void FixedUpdate()
    {
        if (input)
        {
            input = false;
            if (rb.velocity == Vector3.zero)
            {
                rb.AddForce(clone.transform.forward * bulletSpeed);
            }
        }
    }

    void ShootAgain()
    {
        shotAvailable = true;
    }
}
