Update
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AI; // Cần thiết để dùng NavMesh
|
||||
using Hallucinate.Audio;
|
||||
|
||||
[RequireComponent(typeof(NavMeshAgent))] // Tự động thêm component này nếu chưa có
|
||||
public class EnemyAI : MonoBehaviour
|
||||
@@ -28,6 +29,11 @@ public class EnemyAI : MonoBehaviour
|
||||
public float minShootDelay = 1f;
|
||||
public float maxShootDelay = 3f;
|
||||
|
||||
[Header("Audio")]
|
||||
public string alertSound = "Enemy_Alert";
|
||||
public string shootSound = "Enemy_Shoot";
|
||||
private bool hasSpottedPlayer; // Để chỉ kêu alert 1 lần
|
||||
|
||||
private float nextShootTime;
|
||||
private NavMeshAgent agent;
|
||||
|
||||
@@ -37,10 +43,10 @@ public class EnemyAI : MonoBehaviour
|
||||
{
|
||||
agent = GetComponent<NavMeshAgent>();
|
||||
agent.speed = moveSpeed;
|
||||
|
||||
|
||||
// Lưu lại vị trí ban đầu để làm tâm của khu vực tuần tra
|
||||
startPosition = transform.position;
|
||||
|
||||
|
||||
nextShootTime = Time.time + Random.Range(minShootDelay, maxShootDelay);
|
||||
InitBehaviorTree();
|
||||
FindPlayer();
|
||||
@@ -109,9 +115,15 @@ public class EnemyAI : MonoBehaviour
|
||||
|
||||
if (distance <= detectRange)
|
||||
{
|
||||
if (!hasSpottedPlayer)
|
||||
{
|
||||
hasSpottedPlayer = true;
|
||||
AudioManager.Instance?.Play(alertSound, position: transform.position);
|
||||
}
|
||||
return NodeState.Success;
|
||||
}
|
||||
|
||||
hasSpottedPlayer = false; // Reset nếu player ra khỏi tầm mắt
|
||||
return NodeState.Failure;
|
||||
}
|
||||
|
||||
@@ -139,7 +151,7 @@ public class EnemyAI : MonoBehaviour
|
||||
Vector3 randomDirection = Random.insideUnitSphere * patrolRadius;
|
||||
randomDirection += startPosition;
|
||||
NavMeshHit hit;
|
||||
|
||||
|
||||
// Đảm bảo điểm ngẫu nhiên nằm trên bề mặt NavMesh hợp lệ
|
||||
if (NavMesh.SamplePosition(randomDirection, out hit, patrolRadius, 1))
|
||||
{
|
||||
@@ -157,7 +169,7 @@ public class EnemyAI : MonoBehaviour
|
||||
if (player == null) return NodeState.Failure;
|
||||
|
||||
// Debug.Log("Chasing Player");
|
||||
|
||||
|
||||
if (!agent.isActiveAndEnabled || !agent.isOnNavMesh) return NodeState.Failure;
|
||||
|
||||
agent.isStopped = false;
|
||||
@@ -172,7 +184,7 @@ public class EnemyAI : MonoBehaviour
|
||||
if (player == null) return NodeState.Failure;
|
||||
|
||||
// Debug.Log("Focus and Shoot!");
|
||||
|
||||
|
||||
if (!agent.isActiveAndEnabled || !agent.isOnNavMesh) return NodeState.Failure;
|
||||
|
||||
// Dừng NavMeshAgent lại để đứng bắn, tránh bị trượt
|
||||
@@ -202,6 +214,7 @@ public class EnemyAI : MonoBehaviour
|
||||
{
|
||||
if (laserPrefab == null || firePoint == null) return;
|
||||
Instantiate(laserPrefab, firePoint.position, firePoint.rotation);
|
||||
AudioManager.Instance?.Play(shootSound, position: transform.position);
|
||||
// Debug.Log("Laser Shot!");
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.Text;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using UnityEngine.Networking;
|
||||
using Hallucinate.Audio;
|
||||
|
||||
[Serializable]
|
||||
public class Part
|
||||
@@ -44,12 +45,17 @@ public class GerminiNPC : MonoBehaviour
|
||||
public float interactionDistance = 5f; // Khoảng cách tối đa để nói chuyện
|
||||
public Transform playerTransform; // Gán transform của Player vào đây
|
||||
|
||||
[Header("Audio")]
|
||||
public string startTalkSound = "NPC_Interact";
|
||||
public string responseSound = "NPC_Response";
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (Keyboard.current != null && Keyboard.current.fKey.wasPressedThisFrame)
|
||||
{
|
||||
if (CanSeePlayer())
|
||||
{
|
||||
AudioManager.Instance?.Play(startTalkSound, position: transform.position);
|
||||
StartCoroutine(GetGerminiReponse());
|
||||
}
|
||||
else
|
||||
@@ -121,6 +127,7 @@ public class GerminiNPC : MonoBehaviour
|
||||
{
|
||||
var npcResponse = geminiResponse.candidates[0].content.parts[0].text;
|
||||
Debug.Log($"<color=green>Tom:</color> {npcResponse}");
|
||||
AudioManager.Instance?.Play(responseSound, position: transform.position);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
using UnityEngine;
|
||||
using Hallucinate.Audio;
|
||||
|
||||
public class LaserProjectile : MonoBehaviour
|
||||
{
|
||||
public float speed = 5f;
|
||||
public float lifeTime = 5f;
|
||||
|
||||
[Header("Audio")]
|
||||
public string hitSound = "Laser_Hit";
|
||||
|
||||
private void Start()
|
||||
{
|
||||
Destroy(gameObject, lifeTime);
|
||||
@@ -24,6 +28,8 @@ public class LaserProjectile : MonoBehaviour
|
||||
{
|
||||
Debug.Log("Player Hit");
|
||||
|
||||
AudioManager.Instance?.Play(hitSound, position: transform.position);
|
||||
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user