- Home /
(Solved) Why the soldiers stop when they kill the target even if theres targets?
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[c].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[i].GetComponent<DestroyPrefab>().g = gameObject.tag;
shot.GetComponent<GunScript>().Projectiles[i].AddForce (shot.GetComponent<GunScript>().Projectiles[i].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.
else
transform.position = transform.position;
put this in curlies
else
{
transform.position = transform.position;
}
it may not be the problem, but its a damned good idea to do this :P the compiler may be trying to read all of that as an else
statement
bubzy, thanks for helping, but as you said, thats not the problem. The problem is actually whats on the GetNearestTarget function i presume. Becouse the problem is that the soldier is not setting the target. Also, without the curlies or with, its still right. But if it doesnt have curlies, it means that the else will only read the line beneath it. So its still right. Though, thanks for trying to help :)
Can I ask why you're checking target
(on line 64) against a boolean, ins$$anonymous$$d of checking if it's not null? I'm not saying it's the issue, but it's certainly bad practice. And why exactly are you using transform.position = transform.position
? $$anonymous$$aybe there are some weird quirks about Unity scripting that I don't know about yet, but in traditional program$$anonymous$$g that's equivalent to x = 0;
directly followed by x = x;
and then expecting x to be anything other than zero.
Answer by Serinx · Nov 05, 2014 at 08:45 PM
I think I see the problem.
You are setting Transform nearestObj = null outside of the Enemy loop.
I think there is a bit of a disconnect because the first enemy they check is going to be assigned as their nearest object because the other soldiers target will not be null. If this is the nearest enemy, it will remain unchanged.
So when you check the soldiers target, you are comparing this with nearestObj.
On line 41 Shouldn't you be checking otherSoldiers[v].GetComponent().target == ObjPos instead?
Edit: otherSoldiers[v].GetComponent().target == taggedGameObjects[c].transform target is a transform, not a position
Omg it actually worked! The soldiers are a bit querky, but its just becouse of the navmesh, but its easly ignorable. Thanks :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613170738im_/https://answers.unity.com/themes/thub/images/avi.jpg)