Files
BABA_YAGA/Assets/Scripts/AI NPC/LaserProjectile.cs
2026-06-05 23:18:29 +07:00

72 lines
2.4 KiB
C#

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)}");
// Kiểm tra nếu trúng Player
if (other.CompareTag("Player") || other.GetComponentInParent<vIHealthController>() != null)
{
var healthController = other.GetComponentInParent<vIHealthController>();
if (healthController != null)
{
Debug.Log($"<color=red>HIT PLAYER!</color> Found health controller on {healthController.gameObject.name}. Applying {damageAmount} damage.");
var damage = new vDamage(damageAmount);
damage.sender = transform;
damage.hitPosition = transform.position;
healthController.TakeDamage(damage);
}
// Luôn phá hủy đạn khi trúng Player
Impact();
return;
}
// KIỂM TRA LAYER "GROUND"
if (other.gameObject.layer == LayerMask.NameToLayer("Ground"))
{
Debug.Log("<color=yellow>Laser hit GROUND layer.</color>");
Impact();
return;
}
// Phá hủy đạn nếu trúng tường, sàn nhà (mọi thứ không phải trigger khác)
if (!other.isTrigger)
{
Debug.Log("Laser hit an obstacle (Wall/Floor).");
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);
}
}