Enemy firing projectiles through Static Objects

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?

Is the layermask set on the enemies Fighters instance as well?

THAT… is what I missed out on. Apologies, my mistake :sweat_smile: (now it works beautifully, and we’re playing hide and seek :stuck_out_tongue_winking_eye: )

OK so the steps are as follows:

  1. Set all the obstacles in your game as ‘Static’, and their layers as ‘Obstacles’, and bake the NavMeshAgent
  2. Go to the ‘Fighter.cs’ scripts of BOTH YOUR ENEMIES AND PLAYER, and choose the ‘Layer Mask’ (which you put by coding as follows):
[SerializeField] LayerMask layerMask

to be an “Obstacle”
3. Add a box collider to the empty parent gameObject of your obstacles, and ensure the sizes are good (try and error). If they still can fire through houses, make the colliders bigger
4. Inject the code in the title of this topic into your ‘FIghter.cs’ script

Anything else to add? If I ever suffer this problem again, I want to come back here and fix it

[SOLVED]

Speaking of walking though, there’s also a new issue that I’m guessing just got introduced (I promise I’m not trying to make your life harder)… When I press on my dialogue guard to go and talk to him from behind an obstacle, my player looks at him (he’s not supposed to look at the guy when he’s behind the house. He should look at his closest path direction to the guy instead), but doesn’t play the animation to run… He moves in the world, but he doesn’t play the animation it’s supposed to play. I wanted it to seem a little more natural, so that he actually finds the shortest way to get to the other guy, look at that direction he’s moving, play the proper animation, go to the guy he wants to talk to, and then talk to him. In other words:

  1. He should look at his path rather than the target he has to go and talk to
  2. play the movement animation assigned to him, rather than not playing any animations at all (that’s what’s going on right now, when there’s obstacles in his way)
  3. Once he’s clear off obstacles, just naturally keep going to the guy, rather than suddenly play the run animation assigned

Edit: This one was a very easy fix. In ‘PlayerConversant.cs’, there was a ‘transform.LookAt(targetConversant.transform)’ in the ‘void Update()’ function. I commented that line out, and the issue was gone (makes me wonder why it existed in the first place for the player)

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms