using UnityEngine;

public class Bullet : MonoBehaviour
{
    public float speed = 10f;
    public int damage;
    public Vector3 towards;

    void Start(){
        Throwed();
    }

    void Update()
    {
        transform.position = Vector3.MoveTowards(
            transform.position,
            towards,
            speed * Time.deltaTime
        );

        if (transform.position == towards)
        {
            Destroy(gameObject);
        }
    }

    public void Throwed()
    {
        Vector3 player = GameObject.Find("Player").transform.position;
        towards = player + new Vector3(0f, 0.7f, 0f);
    }

    public void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            GameObject.Find("Player").GetComponent<PlayerEvents>().ReceiveDamage(damage);
            Destroy(gameObject);
        }
    }
}

