Question by
Foxy157157 · Jan 05 at 09:31 PM ·
aioptimizationartificial intelligence
Enemy raycast detects player only at constant distance
Hey guys, I have a really strange problem that broke out really unpredictably. At one moment my enemy couldn`t detect player at constant distance (less than 10,25 etc), but now my enemy detects player only at distance == smthing like 1. All measurements and behaviour are initializated in coroutines and adjusting HandleDetection() statement if(distanceToPlayer < fov) doesn`t make anything
public void HandleDetection()
{
Vector3 targetDirection = player.transform.position - transform.position;
float betweenAngle = Vector3.Angle(targetDirection, transform.forward);
if (betweenAngle < 180 * 0.5f)
{
RaycastHit hit;
if (Physics.Raycast(transform.position, targetDirection.normalized, out hit, Mathf.Infinity))
{
Debug.Log("raycast thrown");
if (hit.collider.tag == "Player")
{
Debug.Log("found him");
playerFound = true;
}
}
}
}
Comment