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

public class FrendlyMissle : MonoBehaviour
{
    public float speed = 60;
    public float rotSpeed = 100;
    public Transform target;
    public GameObject explosion;
    public float timer;
    public GameObject trail;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        timer -= Time.deltaTime;

        if (timer <= 0)
        {
            Instantiate(explosion, transform.position, transform.rotation);
            Destroy(gameObject, 0);
        }

        transform.Translate(0, 0, speed * Time.deltaTime);

        if (target != null)
        {
            Vector3 direction = target.position - transform.position;
            Quaternion rotation = Quaternion.LookRotation(direction);
            transform.rotation = Quaternion.Lerp(transform.rotation, rotation, rotSpeed * Time.deltaTime);
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("missle"))
        {
            trail.transform.SetParent(null);
            trail.GetComponent<missleTrail>().StartTimer();
            Instantiate(explosion, transform.position, transform.rotation);
            Destroy(gameObject, 0);
        }
    }
}
