Hello,
I want to achieve that an enemy object starts chasing a player whenever he entered (or collided with) the enemy’s sight arc.
void Update () {
Debug.Log(GetComponent<EnemySight>().targetIsSeen);
if (GetComponent<EnemySight>().targetIsSeen) {
state = State.CHASE;
} else {
state = State.PATROL;
}
}
This is a part of my EnemyController script that changes states with the bool targetIsSeen from the EnemySight script
void FindVisibleTarget() {
Collider[] targetsInViewRadius = Physics.OverlapSphere(transform.position, sightRadius, targetMask);
for (int i = 0; i < targetsInViewRadius.Length; i++) {
Transform target = targetsInViewRadius*.transform;*
Vector3 dirToTarget = target.position - transform.position.normalized;
//target is within fow?
if (Vector3.Angle(transform.forward, dirToTarget) < sightAngle/2) {
float dstToTarget = Vector3.Distance(transform.position, target.position);
if (!Physics.Raycast(transform.position, dirToTarget, dstToTarget, obstacleMask)) {
targetIsSeen = true;
} else {
targetIsSeen = false;
}
}
}
}
This is the code snippet in EnemySight that should determine if a player is within the FOW arc. However, it does not really work that well. Sometimes it does and sometimes it does not.
Am I missing something and other than that, can anybody help me to actually find a way to check whether the player collides with the cone, not only being within it?
I hope I described it sufficiently. I attached the full EnemySight [85894-projectcatchm.png|85894]*
Cheers,
Dan
*
*