This topic is a continuation from this topic, since it got closed. Here is what my problem is:
At first, I had a problem that my player AND NPC can fire stuff through static objects, like a house. We solved this by applying a box collider and introducing a new layerMask known as ‘Obstacle’ in ‘Fighter.cs’, we then applied it to the static objects that stand between us and our enemies, and then we wrote this code:
bool HasLineOfSight() {
Ray ray = new Ray(transform.position + Vector3.up, target.transform.position - transform.position);
if (Physics.Raycast(ray, out RaycastHit hit, Vector3.Distance(transform.position, target.transform.position - transform.position), layerMask)) {
return false;
}
return true;
}
and in ‘Fighter.AttackBehaviour()’, at the top of it, I introduced this function:
if (!HasLineOfSight() && currentWeaponConfig.HasProjectile()) {
while (!HasLineOfSight() && GetComponent<Mover>().CanMoveTo(target.transform.position)) {
GetComponent<Mover>().MoveTo(target.transform.position, 1);
return;
}
}
However, it only solved the problem for the player, but not for the NPCs with projectile weapons. Unfortunately they still fire weapons at me through static objects, even if the stuff between me and them have box colliders, and these collider holders are labelled with the ‘Obstacle’ layer masks, and frankly speaking, it’s driving me nuts why this is happening… What else can I change to get this to work?