Hello all, first time asking for help here. I can not get my existing enemy controller script to jive with any suggestion of how to make sure enemies cant shoot through walls. In the only one Ive been able to put together, the enemy begins facing me as soon as the game starts and they’re able to shoot through walls. It’s made everything into a big mess. Everything else is perfect however so Id like to preserve to existing EnemyController script. I know at void update there’s just some sort of an if statement that would work here, so that they raycast and only fire if they hit something tagged “Player”. Any help would really mean a lot, thanks!
This is the GuardAI script that goes onto each enemy NPC:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GuardAI : MonoBehaviour
{
public GameObject ThePlayer;
public float TargetDistance;
public float AllowedRange = 5;
public GameObject TheEnemy;
public float EnemySpeed;
public int AttackTrigger;
public RaycastHit Shot;
public int DealingDamage;
public AudioSource GuardAttackSound;
public AudioSource GuardAttackNotice;
private bool inRage = false;
void Update()
{
transform.LookAt(ThePlayer.transform);
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out Shot, 100))
{
TargetDistance = Shot.distance;
if (TargetDistance <= AllowedRange)
{
EnemySpeed = 0.01f;
if (AttackTrigger == 0)
{
TheEnemy.GetComponent<Animation>().Play("walking");
transform.position = Vector3.MoveTowards(transform.position, ThePlayer.transform.position, EnemySpeed);
}
}
else
{
EnemySpeed = 0;
TheEnemy.GetComponent<Animation>().Play("idle");
}
}
if (AttackTrigger == 1)
{
if (DealingDamage == 0)
{
GuardAttackNotice.Play();
EnemySpeed = 0;
TheEnemy.GetComponent<Animation>().Play("attack");
StartCoroutine(TakingDamage());
}
}
}
void OnTriggerEnter()
{
AttackTrigger = 1;
}
void OnTriggerExit()
{
AttackTrigger = 0;
}
IEnumerator TakingDamage()
{
GuardAttackSound.Play();
DealingDamage = 1;
yield return new WaitForSeconds(1);
if (GuardEnemy.GlobalGuard != 6)
{
HealthMonitor.HealthValue -= 2;
}
yield return new WaitForSeconds(3);
DealingDamage = 0;
}
}
I appreciate the reply but this wasn’t exactly what I was looking for. I have gotten past that point. The problem is that the enemy raycasts through walls and still shoots the player from another room. Not that the raycasting doesn’t work at all.
P.S. Love Jimmy Vegas, unfortunately, none of his tutorials touch upon this at all.
Unfortunately no luck with this still. I thought it would be something with a more popular method of some sort. Im really out of ideas here. I’ve redone the entire AI system so many times to try one method over another.
First thing to point out is that your trigger methods do not have have the correct signature:
void OnTriggerEnter() { ... }
// should actually be
void OnTriggerEnter(Collider collider) { ... }
The same applies to the OnTriggerExit.
Next thing to point out is that your “AttackTrigger” will be set to “1”, no matter which trigger is going to cause the interaction (once the signature has been fixed). You might want to add some conditions in order to filter the interacting physics objects.
Last but not least, the raycast doesn’t do anything that prevents the enemy from shooting your player, since your shooting logic only relies on “AttackTrigger” being 1 (and the DealingDamage value, but let’s forget about it for a minute).You could replace both with booleans if you only need 2 states, just as a small side note.
Your raycast needs to determine what sort of object has been hit (either by tag-comparison or a call to GetComponent with the player component that sort of qualifies the hit target as the player), then - and only when this is true - continue handling the shooting logic.
Thank you so much for this thoughtful response! Taking the time really helps me get it down fundamentally so I can actually learn this for the future rather than patch something together for just this instance. The last portion, about making sure the enemy raycast has hit something, and knows what that is, is what Im working on learning about now. That will solve the last of my problems for the game I made if I can figure that out from the enemy’s perspective.
I made things so much worse. I think I’m going to try starting over with a completely new idea or technique. If anyone has any ideas I would love to hear an opinion on where to start with an enemy AI enemy shooting script that doesn’t ignore walls and obstacles completely. This project is on ice until I can work out enemy raycasting.