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

public class BulletMoving : MonoBehaviour
{
    public DataBullet dataBullet;
    
    public float lifetime = 3f;
    private Rigidbody rb;

    private Vector3 direction;

    private float time = 0f;

    private Ray ray;
    private RaycastHit hit;

    private void die(float lifetime){
        if(time > lifetime){
            GameObject.Destroy(this.gameObject);
        }
    }
    private void CheckForColliders(){
        if(Physics.Raycast(ray, out hit)){ //wenn true wird durch out in variable hit ein RayCastHit gespeichert
            direction = hit.point - transform.position;//Richtung in die, die Kugel fliegt
        }
    }
    // Start is called before the first frame update
    void Start()
    {
        rb = this.GetComponent<Rigidbody>();
        
        
        ray = Camera.main.ViewportPointToRay(new Vector3(0.5f,0.5f,0f));
        CheckForColliders();
    }
    

    // Update is called once per frame
    void Update()
    {
        rb.velocity = direction * dataBullet.speed;
        die(lifetime);
        time += Time.deltaTime;
    }
}
