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

public class Bullet : MonoBehaviour
{
    private GameObject playerObj;
    private GameObject enemyObj;
    private Vector2 vec2;
    private float speedData;
    private Rigidbody2D rb2;
    
    // Start is called before the first frame update
    void Start()
    {
        //3秒後に消す
        Destroy(gameObject,3.0f);
        
        //コンポーネント取得
        rb2 = GetComponent<Rigidbody2D>();
        
        //スピード係数
        speedData = 5.0f;
        
        //プレイヤーと敵のオブジェクト取得
        playerObj = GameObject.Find("Player");
        enemyObj = GameObject.Find("Enemy");
    }

    // Update is called once per frame
    void Update()
    {
        //移動
        rb2.velocity = new Vector2(vec2.x * speedData, vec2.y * speedData);
        
        //当たり判定
        float x = playerObj.transform.position.x - transform.position.x;
        float y = playerObj.transform.position.y - transform.position.y;
        float d = Mathf.Sqrt( (x * x) + (y * y) );
        float r1 = 0.35f;
        float r2 = 0.2f;

        if (d < r1 + r2)
        {
            Destroy(gameObject);
        }
    }

    public void Init(float angle)
    {
        //弾を飛ばす角度を設定する
        //Xの移動量はvec2.xに格納してください
        //Yの移動量はvec2.yに格納してください
        vec2.x = 0;
        vec2.y = 0;
    }
}