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

public class Bullet : MonoBehaviour {

    public Transform lightObj;

    private Vector3 dir;
    private Vector3 pos;

    // Use this for initialization
    void Start () {
        pos = transform.position;
	}

    // Update is called once per frame
    Vector3 oldVel;

    void Update () {
        dir = transform.position - pos;
        dir.Normalize();
        oldVel = transform.parent.GetComponent<Rigidbody>().velocity;
	}

    void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Wall" || other.tag == "Enemy")
        {
            
            Transform clone;
            var child = transform.parent.FindChild("Bullet Light");
            float intensity = child.GetComponent<Light>().intensity;

            /*RaycastHit hit;                                   ----PATCH----

            if(Physics.Raycast(transform.position,dir,out hit))
            {
                oldVel = Vector3.Reflect(oldVel, hit.normal);
            }
            clone.GetComponent<Rigidbody>().velocity = oldVel;
            */

            clone = Instantiate(lightObj, transform.position - dir * (other.tag == "Enemy" ? 1.25f : 0.5f), transform.rotation);
            clone.GetComponent<Light>().intensity = intensity;
        }
    }
}
