How do I check if a GameObject is within or colliding with an arc?
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[i].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 link text and a picture
Cheers, Dan
Answer by DaDarkDan · Jan 16, 2017 at 06:19 PM
Ok I found the answer. The problem was that the direction to the target was wrong, so i changed that to:
Vector3 dirToTarget = target.position - transform.position;
Your answer
Follow this Question
Related Questions
GameObject not colliding properly with a collider! 3 Answers
Shoot towards mouse except mouse y 1 Answer
How to Trigger/collision only once time per gameobject hit's? 0 Answers
Collisions not working in top-down RPG? 0 Answers
No overload for method. 1 Answer