using UnityEngine; using Hallucinate.Audio; using Invector; public class LaserProjectile : MonoBehaviour { public float speed = 15f; // Tăng tốc độ đạn để cảm giác mượt hơn public float lifeTime = 5f; public int damageAmount = 10; [Header("Audio")] public string hitSound = "Laser_Hit"; private void Start() { // Tự hủy sau một khoảng thời gian nếu không trúng gì Destroy(gameObject, lifeTime); } private void Update() { // Di chuyển đạn transform.position += transform.forward * speed * Time.deltaTime; } private void OnTriggerEnter(Collider other) { // Debug: Log tên và tag của bất cứ thứ gì đạn chạm vào Debug.Log($"Laser collided with: {other.name} | Tag: {other.tag} | Layer: {LayerMask.LayerToName(other.gameObject.layer)}"); // 1. Kiểm tra nếu trúng Player hoặc đối tượng có Health var healthController = other.GetComponentInParent(); if (other.CompareTag("Player") || healthController != null) { if (healthController != null) { Debug.Log($"HIT PLAYER! Applying {damageAmount} damage."); var damage = new vDamage(damageAmount); damage.sender = transform; damage.hitPosition = transform.position; healthController.TakeDamage(damage); } Impact(); return; } // 2. Phá hủy đạn nếu trúng Ground, Tường, hoặc bất kỳ vật thể đặc nào (không phải Trigger) if (!other.isTrigger) { Debug.Log($"Laser hit solid object: {other.name} (Ground/Obstacle). Destroying."); Impact(); } } private void Impact() { // Chạy âm thanh AudioManager.Instance?.Play(hitSound, position: transform.position); // Phá hủy đạn ngay lập tức Destroy(gameObject); } }