im trying to make a AI agent that can hear stuff, but the ai just wont hear anything. heres my code. please help. Code (CSharp): using UnityEngine; using UnityEngine.AI; public class EnemyAI : MonoBehaviour { public NavMeshAgent agent; public Transform player; public LayerMask whatIsGround, whatIsPlayer; //Patroling public Vector3 walkPoint; bool walkPointSet; public float walkPointRange; //Attacking public float timeBetweenAttacks; bool alreadyAttacked; public GameObject projectile; //States public float sightRange, attackRange; public float fieldOfViewAngle = 160f; public bool playerInSightRange, playerInAttackRange; public bool isDisabled = false; //Sound detection public float hearingRange = 200f; public float hearingThreshold = 2.5f; public void ToggleAi(){ isDisabled = !isDisabled; } private void Awake() { player = GameObject.Find("PlayerObj").transform; agent = GetComponent<NavMeshAgent>(); } private void Update() { if (isDisabled) { agent.SetDestination(gameObject.transform.position); return; } // Check for sight and attack range playerInSightRange = Physics.CheckSphere(transform.position, sightRange, whatIsPlayer) && CanSeePlayer() && !IsPlayerObstructed(); playerInAttackRange = Physics.CheckSphere(transform.position, attackRange, whatIsPlayer) && CanSeePlayer() && !IsPlayerObstructed(); if (CanSeePlayer()) { // Player is visible, use their real position lastKnownPlayerPosition = player.position; hasseenplayeryet = true; } // Get off ramp if necessary if (IsOnRamp()) { GetOffRamp(); return; } // Check for sound within hearing range and investigate if necessary // Check for sound within hearing range and investigate if necessary Collider[] colliders = Physics.OverlapSphere(transform.position, hearingRange); foreach (Collider collider in colliders) { AudioSource audioSource = collider.GetComponent<AudioSource>(); if (audioSource != null && audioSource.isPlaying) { float distance = Vector3.Distance(transform.position, collider.transform.position); if (distance <= hearingThreshold) { // Investigate the sound Vector3 soundDirection = audioSource.transform.position - transform.position; walkPoint = audioSource.transform.position; walkPointSet = true; } } } if (!playerInSightRange && !playerInAttackRange) { Patroling(); } if (playerInSightRange && !playerInAttackRange) { ChasePlayer(); } if (playerInAttackRange && playerInSightRange) { AttackPlayer(); } } private bool IsOnRamp() { RaycastHit hit; if (Physics.Raycast(transform.position, -transform.up, out hit, 2f, whatIsGround)) { return hit.collider.CompareTag("Ramp"); } return false; } Vector3 lastKnownPlayerPosition; private void GetOffRamp() { // Determine which way to go based on player's position Vector3 playerDirection = player.position - transform.position; Vector3 rampNormal = transform.up; Vector3 perpendicular = Vector3.Cross(playerDirection, rampNormal); // Check if player is visible if (CanSeePlayer()) { // Player is visible, use their real position lastKnownPlayerPosition = player.position; agent.SetDestination(lastKnownPlayerPosition * walkPointRange); } else if (hasseenplayeryet) { // Player is not visible but we have a last known position, use that agent.SetDestination((Vector3) lastKnownPlayerPosition); } else { // Player is not visible and we don't have a last known position, use walk point agent.SetDestination(walkPoint); } } private bool CanSeePlayer() { Vector3 direction = player.position - transform.position; float angle = Vector3.Angle(direction, transform.forward); if (angle < fieldOfViewAngle * 0.5f) { RaycastHit hit3; if (Physics.Raycast(transform.position, direction.normalized, out hit3, sightRange)) { if (hit3.collider.CompareTag("Player")) { return true; } } } return false; } private bool IsPlayerObstructed() { RaycastHit hit2; if (Physics.Raycast(transform.position, player.position - transform.position, out hit2, sightRange)) { if (!(hit2.collider.gameObject.layer == LayerMask.NameToLayer("Player"))) { return true; } } if (Physics.Raycast(transform.position, player.position - transform.position, out hit2, attackRange)) { if (!(hit2.collider.gameObject.layer == LayerMask.NameToLayer("Player"))) { return true; } } return false; } private void Patroling() { if (isDisabled){ agent.SetDestination(gameObject.transform.position); return; } if (!walkPointSet) SearchWalkPoint(); if (walkPointSet) agent.SetDestination(walkPoint); Vector3 distanceToWalkPoint = transform.position - walkPoint; //Walkpoint reached if (distanceToWalkPoint.magnitude < 1f) walkPointSet = false; } private void SearchWalkPoint() { if (isDisabled){ agent.SetDestination(gameObject.transform.position); return; } //Calculate random point in range float randomZ = Random.Range(-walkPointRange, walkPointRange); float randomX = Random.Range(-walkPointRange, walkPointRange); walkPoint = new Vector3(transform.position.x + randomX, transform.position.y, transform.position.z + randomZ); if (Physics.Raycast(walkPoint, -transform.up, 2f, whatIsGround)) walkPointSet = true; } private bool hasseenplayeryet; private bool obstacleInWay; public void UpdateLocation(){ if (isDisabled){ agent.SetDestination(gameObject.transform.position); return; } walkPoint = lastKnownPlayerPosition; agent.SetDestination(walkPoint); } private void ChasePlayer() { if (isDisabled){ agent.SetDestination(gameObject.transform.position); return; } Vector3 direction = player.position - transform.position; float angle = Vector3.Angle(direction, transform.forward); // Check if there are any obstacles between the enemy and the player if (!Physics.Linecast(transform.position, player.position, out RaycastHit hit, ~whatIsPlayer)) { if (hit.collider.CompareTag("Player")) { // Player is visible, no obstacles in the way obstacleInWay = false; } else { // Something else is blocking the enemy's view of the player obstacleInWay = true; } } else { // Nothing is blocking the enemy's view of the player obstacleInWay = false; } if (angle < fieldOfViewAngle * 0.5f) { RaycastHit hit1; if (Physics.Raycast(transform.position, direction.normalized, out hit1, sightRange)) { if (hit.collider.CompareTag("Player")){ if (hasseenplayeryet){ agent.SetDestination(lastKnownPlayerPosition); } else { player.transform.position = lastKnownPlayerPosition; agent.SetDestination(lastKnownPlayerPosition); hasseenplayeryet = true; } } } } else { if (hasseenplayeryet) { if (!obstacleInWay) { // No obstacles in the way, chase the player lastKnownPlayerPosition = player.position; agent.SetDestination(lastKnownPlayerPosition); } else { // Obstacle in the way, set walkPoint to player's position walkPoint = lastKnownPlayerPosition; } } Patroling(); } } private void AttackPlayer() { if (isDisabled){ agent.SetDestination(gameObject.transform.position); return; } // Make sure enemy doesn't move agent.SetDestination(transform.position); // Check line of sight to player Vector3 direction = player.position - transform.position; RaycastHit hit; if (Physics.Raycast(transform.position, direction, out hit, attackRange, ~whatIsGround)) { if (hit.collider.CompareTag("Player")) { // Attack player transform.LookAt(player); if (!alreadyAttacked) { // Attack code here Rigidbody rb = Instantiate(projectile, transform.position, Quaternion.identity).GetComponent<Rigidbody>(); rb.AddForce(transform.forward * 32f, ForceMode.Impulse); rb.AddForce(transform.up * 8f, ForceMode.Impulse); // End of attack code alreadyAttacked = true; Invoke(nameof(ResetAttack), timeBetweenAttacks); } return; } } // If player is not in sight, reset the attack status and chase the player alreadyAttacked = false; ChasePlayer(); } private void ResetAttack() { alreadyAttacked = false; } private void OnDrawGizmosSelected() { Gizmos.color = Color.red; Gizmos.DrawWireSphere(transform.position, attackRange); Gizmos.color = Color.yellow; Gizmos.DrawWireSphere(transform.position, sightRange); Gizmos.color = Color.green; Gizmos.DrawWireSphere(transform.position, hearingRange); Vector3 fovLine1 = Quaternion.AngleAxis(fieldOfViewAngle * 0.5f, transform.up) * transform.forward * sightRange; Vector3 fovLine2 = Quaternion.AngleAxis(-fieldOfViewAngle * 0.5f, transform.up) * transform.forward * sightRange; Gizmos.color = Color.blue; Gizmos.DrawRay(transform.position, fovLine1); Gizmos.DrawRay(transform.position, fovLine2); if (walkPointSet) { Gizmos.color = Color.green; Gizmos.DrawSphere(walkPoint, 0.2f); } } }