How to keep track of multiple NPC's / Enemies
Hi there,
I am making a defence game, where the player places soldiers which can be moved along the walls/buildings etc
Each soldier has a light of sight in the form of an arc, if an enemy enters the line of sight, defined by a fixed amount of degrees and range, they will engage that enemy until they have left the line of sight or are dead.
I have that working, however, I am only tracking one enemy at a time. The game is going to have large numbers of each type of enemy on screen.
Which is the best way to track these, and which is the best way to pass the information to the soldier that they can shoot that particular enemy i.e. they won't be able to shoot an enemy that flies, unless they have a particular weapon.
Currently I have a variable for each enemy, but the problem arises with targeting more than one. I have included my script below.
using UnityEngine;
using System.Collections;
public class SODLineOfSightCol : MonoBehaviour {
public float fieldOfViewAngle = 110.0f; //what angle of the sphere collider will be used
public bool npcInSight; //is the npc in sight -- may need to be changed to look for a Rigidbody with a certain tag or layer
private SphereCollider col;
GameObject[] npc;
float soldierRotationTime = 3.5f;
LineRenderer line;
void Awake () {
closest = null;
col = GetComponent<SphereCollider>(); //find the sphere collider
npc = GameObject.FindGameObjectsWithTag("npc");
Debug.Log(npc1);
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//get soldiers original rotation
//this will be used to reset after it has killed an enemy
//check if the NPC is still alive, if so, turn to face and shoot etc...
//find the closest enemy to the soldier
//FindClosestEnemy();
}
void OnTriggerStay(Collider other) { //called once per game for every Collider that is touching the trigger
//get 2x Vector 3's, one fo the angle of the Vector to the Player
//another for the Soldiers Forward Vector
Vector3 direction = other.transform.position - transform.position;
float angle = Vector3.Angle(direction, transform.forward);
if(angle < fieldOfViewAngle * 0.5f){ //check if the angle is less than half of the fieldOfViewAngle
//create a ray to see if an object is blocking the line of sight
RaycastHit hit;
//if(Physics.Raycast(transform.position + transform.up, direction.normalized, out hit, col.radius)){
if(Physics.Raycast(transform.position, direction.normalized, out hit, col.radius)){
//transform.up is used to move the ray up 1 unit
//direction.normalized is used to keep a vector in the same direction, but its length at 1.0f
//col.radius is used to keep the ray the length of the sphere colliders radius, as there is no need for it to be longer
if(hit.collider.gameObject == npc){
npcInSight = true;
//inform solider to face the npc
//call funciton to start shooting
Transform targetNPC = npc.transform;
Quaternion targetRotation = Quaternion.LookRotation(targetNPC.position - transform.position);
//Quaternion.LookRotation(targetNPC.position - transform.position); finds the direction from the transform.position to target.position
float strength = Mathf.Min(soldierRotationTime*Time.deltaTime,1);
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, strength);
}
}
}
}
void OnTriggerExit(Collider other){
if(other.gameObject == npc)
npcInSight = false;
}
}
I have tried to use, FindObjectsWithTags in the hope it will be able to use all of the enemies I have tagged with 'npc' but it just seems to chuck up this error "`==' cannot be applied to operands of type UnityEngine.GameObject' and
UnityEngine.GameObject[]'"
Apologies for all the comments etc... Any help greatly appreciated!
Your answer
![](https://koobas.hobune.stream/wayback/20220612082932im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Creating multiple text components on a canvas 1 Answer
Enemy AI Error 3 Answers