Files
BABA_YAGA/Assets/Scripts/AI NPC/LaserProjectile.cs

75 lines
2.4 KiB
C#
Raw Normal View History

2026-06-03 13:42:09 +07:00
using UnityEngine;
2026-06-04 23:01:39 +07:00
using Hallucinate.Audio;
2026-06-05 22:38:32 +07:00
using Invector;
2026-06-03 13:42:09 +07:00
public class LaserProjectile : MonoBehaviour
{
2026-06-05 22:38:32 +07:00
public float speed = 15f; // Tăng tốc độ đạn để cảm giác mượt hơn
2026-06-03 13:42:09 +07:00
public float lifeTime = 5f;
2026-06-05 22:38:32 +07:00
public int damageAmount = 10;
2026-06-03 13:42:09 +07:00
2026-06-04 23:01:39 +07:00
[Header("Audio")]
public string hitSound = "Laser_Hit";
2026-06-03 13:42:09 +07:00
private void Start()
{
2026-06-05 22:38:32 +07:00
// Tự hủy sau một khoảng thời gian nếu không trúng gì
2026-06-03 13:42:09 +07:00
Destroy(gameObject, lifeTime);
}
private void Update()
{
2026-06-05 22:38:32 +07:00
// Di chuyển đạn
transform.position += transform.forward * speed * Time.deltaTime;
2026-06-03 13:42:09 +07:00
}
private void OnTriggerEnter(Collider other)
{
2026-06-05 22:38:32 +07:00
// 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)}");
2026-06-03 13:42:09 +07:00
2026-06-05 22:54:37 +07:00
// 1. Kiểm tra nếu trúng Player hoặc đối tượng có Health
var healthController = other.GetComponentInParent<vIHealthController>();
if (other.CompareTag("Player") || healthController != null)
2026-06-05 22:38:32 +07:00
{
if (healthController != null)
{
2026-06-05 22:54:37 +07:00
Debug.Log($"<color=red>HIT PLAYER!</color> Applying {damageAmount} damage.");
2026-06-05 22:38:32 +07:00
var damage = new vDamage(damageAmount);
damage.sender = transform;
damage.hitPosition = transform.position;
healthController.TakeDamage(damage);
}
Impact();
return;
}
2026-06-04 23:01:39 +07:00
2026-06-06 00:00:27 +07:00
// 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)
2026-06-05 22:54:37 +07:00
// 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)
2026-06-06 00:00:27 +07:00
2026-06-05 22:38:32 +07:00
if (!other.isTrigger)
{
2026-06-05 22:54:37 +07:00
Debug.Log($"Laser hit solid object: {other.name} (Ground/Obstacle). Destroying.");
2026-06-05 22:38:32 +07:00
Impact();
2026-06-03 13:42:09 +07:00
}
}
2026-06-05 22:38:32 +07:00
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);
}
2026-06-03 13:42:09 +07:00
}