I am pretty new to unity and now working with enemy AI for my game. I made some progress and added attack, chase and patroling states but now I am stuck with one issue where enemy tries to shoot through walls. Is there any way to fix this? Here is my enemy AI script:
using UnityEngine;
using UnityEngine.AI;
public class EnemyAi : MonoBehaviour
{
public NavMeshAgent agent;
public Transform player;
public LayerMask whatIsGround, whatIsPlayer;
public int maxHealth;
public int currentHealth;
Ragdoll ragdoll;
public bool isDead;
//Patroling
public Vector3 walkPoint;
bool walkPointSet;
public float walkPointRange;
//Attacking
public float timeBetweenAttacks;
bool alreadyAttacked;
public GameObject projectile;
public Transform projectilePos;
//States
public float sightRange, attackRange;
public bool playerInSightRange, playerInAttackRange;
Animator animator;
private void Awake()
{
currentHealth = maxHealth;
player = GameObject.Find("Player").transform;
agent = GetComponent<NavMeshAgent>();
animator = GetComponent<Animator>();
ragdoll = GetComponent<Ragdoll>();
}
private void Update()
{
//Check for sight and attack range
playerInSightRange = Physics.CheckSphere(transform.position, sightRange, whatIsPlayer);
playerInAttackRange = Physics.CheckSphere(transform.position, attackRange, whatIsPlayer);
if (!playerInSightRange && !playerInAttackRange) Patroling();
if (playerInSightRange && !playerInAttackRange) ChasePlayer();
if (playerInAttackRange && playerInSightRange) AttackPlayer();
animator.SetFloat("Speed", agent.velocity.magnitude);
}
private void Patroling()
{
if (!walkPointSet) SearchWalkPoint();
if (walkPointSet) agent.SetDestination(walkPoint);
Vector3 distanceToWalkPoint = transform.position - walkPoint;
//Walkpoint reached
if (distanceToWalkPoint.magnitude < 0.1f) walkPointSet = false;
}
private void SearchWalkPoint()
{
//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 void ChasePlayer()
{
agent.SetDestination(player.position);
}
private void AttackPlayer()
{
//Make sure enemy doesn't move
agent.SetDestination(transform.position);
transform.LookAt(player);
if (!alreadyAttacked)
{
//Attack code here
Rigidbody rb = Instantiate(projectile, projectilePos.position, projectilePos.rotation).GetComponent<Rigidbody>();
rb.AddForce(transform.forward * 32f, ForceMode.Impulse);
rb.AddForce(transform.up * 0f, ForceMode.Impulse);
animator.SetTrigger("Shoot");
alreadyAttacked = true;
Invoke(nameof(ResetAttack), timeBetweenAttacks);
}
}
private void ResetAttack()
{
alreadyAttacked = false;
}
public void TakeDamage(int damage)
{
currentHealth -= damage;
if (currentHealth <= 0)
{
Die();
}
}
private void Die()
{
isDead = true;
EnemyAi enemyMovement = GetComponent<EnemyAi>();
NavMeshAgent enemyMovement2 = GetComponent<NavMeshAgent>();
if (enemyMovement != null)
{
enemyMovement.enabled = false;
enemyMovement2.enabled = false;
}
ragdoll.ActivateRagdoll();
Invoke("Delay", 5f);
}
private void Delay()
{
Destroy(gameObject);
}
}