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

public class ShooterGun : MonoBehaviour
{
    // Start is called before the first frame update
    public GameObject BulletPrefab, SpawnPoint;
    float cooltime;

    void Start()
    {
        // transform.parent = GameObject.Find("GunHolder").transform;
        cooltime = Time.time;
    }

    private void FixedUpdate()
    {
            if (GameObject.Find("PainKiller").transform.position.y < transform.position.y)
            {
                transform.position = new Vector3(transform.position.x, transform.position.y, -2);
            }
            if (GameObject.Find("PainKiller").transform.position.y > transform.position.y)
            {
                transform.position = new Vector3(transform.position.x, transform.position.y, 1);
            }
      
            Vector3 dir = Camera.main.WorldToScreenPoint(GameObject.Find("PainKiller").transform.position) - Camera.main.WorldToScreenPoint(transform.position);
            float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
            transform.parent.transform.rotation = Quaternion.AngleAxis(angle + 180, Vector3.forward);
       
        
            if (cooltime < Time.time)
            {
                GameObject bullet = Instantiate(BulletPrefab, SpawnPoint.transform.position, Quaternion.Euler(0, 0, 0));
                Vector3 ShootAngle = GameObject.Find("PainKiller").transform.position - SpawnPoint.transform.position;
                bullet.GetComponent<Rigidbody2D>().velocity = ShootAngle * 5f;
                float number = Random.Range(3f, 5f);
                cooltime = Time.time + number;
            }
    }
}
