using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SoldierScript : MonoBehaviour {
public float Speed;
public int RotationSpeed;
public PauseScript pause;
public string BRName;
public Rigidbody projectile;
public Transform Shooter;
public float stopOnDistance;
public float ShootDistance;
public float shootForce;
public float reloadTime;
public Transform target;
NavMeshAgent agent;
public GameObject[] taggedGameObjects;
private bool reloadNeeded = false;
private bool stop;
IEnumerator Reload(){
yield return new WaitForSeconds (reloadTime);
reloadNeeded = false;
stop = false;
}
//get the target area
void GetNearestTaggedObject() {
taggedGameObjects = GameObject.FindGameObjectsWithTag("Enemy");
float nearestDistanceSqr = Mathf.Infinity;
GameObject[] otherSoldiers = GameObject.FindGameObjectsWithTag("Soldier");
Transform nearestObj = null;
for(int c = 0;c<taggedGameObjects.Length;c++)
{
Vector3 ObjPos = taggedGameObjects
.transform.position;
float distancesqr = (ObjPos - transform.position).sqrMagnitude;
bool SoldierTargetTrue = false;
for(int v = 0;v<otherSoldiers.Length;v++){
if(otherSoldiers[v].GetComponent<SoldierScript>().target == nearestObj&&otherSoldiers.Length<=taggedGameObjects.Length&&otherSoldiers[v] != gameObject)
SoldierTargetTrue = true;
}
if (distancesqr < nearestDistanceSqr&&!taggedGameObjects[c].GetComponent<EnemyScript>().OnBridge&&!SoldierTargetTrue) {
nearestObj = taggedGameObjects[c].transform;
nearestDistanceSqr = distancesqr;
}
}
target = nearestObj;
}
void Awake(){
pause = GameObject.Find ("Character").GetComponent<PauseScript>();
agent = gameObject.GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void Update () {
transform.position = new Vector3 (transform.position.x,1.0059f,transform.position.z);
GetNearestTaggedObject();
if(target == true){
if((transform.position - target.position).sqrMagnitude > stopOnDistance){
agent.SetDestination(target.position);
}
else
transform.position = transform.position;
if((transform.position - target.position).sqrMagnitude < ShootDistance&&pause.paused == false){
Vector3 toTarget = target.position - transform.position;
toTarget.y = 0;
transform.forward = Vector3.Slerp(transform.forward, toTarget, RotationSpeed * Time.deltaTime);
if(reloadNeeded == false){
reloadNeeded = true;
gameObject.audio.Play ();
Rigidbody shot = Instantiate (projectile, Shooter.position, Shooter.rotation) as Rigidbody;
for(int i = 0; i<shot.GetComponent<GunScript>().Projectiles.Length;i++){
shot.GetComponent<GunScript>().Projectiles*.GetComponent<DestroyPrefab>().g = gameObject.tag;
shot.GetComponent<GunScript>().Projectiles*.AddForce (shot.GetComponent<GunScript>().Projectiles*.transform.forward * shootForce);
}
}
}
}
if (reloadNeeded&&stop == false) {
StartCoroutine(Reload());
stop = true;
}
}
}
Ok heres the script. The main part of the script is the GetNearestTaggedObject function. There, it gaets all of the enemies currently on the scene, then check if other soldiers that are also are in the scene doesnt have the same tagged object. Then he sets the nearest valid taggedgameobject and then set the target. But for some reason, all of the soldiers currently in the scene sets the target as the same enemy, and then they just stop attacking the enemy and do nothing, even while theres five enemies in the scene. Help.
Update
I made the code a bit simple to understand, so i could do more stuffs witch i could understand what im doing wrong. Still, im having the same problem. Also i added so the soldiers dont get enemies that are on bridges, and it works, so no need to be wasting time about that.