I am currently fixing the last part of an enemy AI so it won’t try to shoot the player through walls. I have attempted to do this by having a raycast go from the enemy to the player detecting if there is something in between but for some reason, it does not work and I really have no idea why. Anyone out there who could shed some light on this problem?
RaycastHit hit;
private void Update()
{
playerInRange = Physics.CheckSphere(transform.position, attackRange, whatIsPlayer);
if (Physics.Raycast(shootPos.position, (player.position - transform.position), out hit, attackRange) && hit.transform.CompareTag("Player"))
{
//DOES NOT REACH HERE
Debug.Log(hit.transform.tag);
agent.enabled = false;
AttackPlayer();
}
else
{
if (agent.enabled == false)
{
agent.enabled = true;
}
ChasePlayer();
}
}
Hmm, it does actually hit the player sometimes if you stand in certain spots but most of the time the raycast seems to hit the floor or random objects even if the player object is right in front.