using UnityEngine;

public class BulletProjectile : MonoBehaviour {
    [SerializeField] private TrailRenderer trailRenderer;
    [SerializeField] private Transform bulletHitVfxTransform;
    [SerializeField] private float moveSpeed = 200f;
    private Vector3 targetPosition;
    public void Setup(Vector3 newTargetPosition) => targetPosition = newTargetPosition;

    private void Update() {
        Vector3 moveDir = (targetPosition - transform.position).normalized;
        float distanceBeforeMoving = Vector3.Distance(transform.position, targetPosition);
        transform.position += moveSpeed * Time.deltaTime * moveDir;
        float distanceAfterMoving = Vector3.Distance(transform.position, targetPosition);
        
        if (!(distanceBeforeMoving < distanceAfterMoving)) return;
        transform.position = targetPosition;
        trailRenderer.transform.parent = null;
        Destroy(gameObject);
        Instantiate(bulletHitVfxTransform, targetPosition, Quaternion.identity);
    }
}
