Hello, i have a script were theres a SerDestination on it. Though the SetDestination only works for gameobjects tagged as Enemy, but for the rest, it doesnt work. When a gameobject is tagged other thing then Enemy, the SetDestination doesnt work (though i already changed the target of the SetDestination to the correct tag), but if i change the exact game object tag to enemy (i also change SetDestination target to objects with tag enemy), it works perfectly. Any help? Is this an possible unity bug?
agent.SetDestination(GameObject.FindGameObjectWithTag("Core").transform.position); // set new destination, to the core
This code actually works when its:
agent.SetDestination(GameObject.FindGameObjectWithTag("Enemy").transform.position); // set new destination, to the core
Yes, i changed the name of the gameobject and stuff to “Enemy”/“Core”, but still, only “Enemy” works.
Heres the link for a video i made about this glitch
Entire Script:
using UnityEngine;
using System.Collections;
public class SoldierScript : MonoBehaviour {
public float Speed; //Character speed
public int RotationSpeed; //Character Rotation speed
public PauseScript pause; //Pause script
public string BRName; //Name of the character in portuguese
public int Health; //Health
public int MaxHealth; //Max health
public Rigidbody projectile; //Projectile that he will be shooting
public Transform Shooter; //Were this projectile will be shooting
public float stopOnDistance; //Distance of character and enemy that the character will stop
public float ShootDistance; //Distance that hes allowed to shoot
public float shootForce; //Force of the projectile
public float reloadTime; //Delay of the projectile shot
public Transform target; //Enemy
public GameObject particle; //Particle
public TextMesh text; //Health text mesh
public GameObject Core;
NavMeshAgent agent; //NavMeshAgent
public GameObject[] taggedGameObjects; //All of the enemys
private bool reloadNeeded = false; //Does it needs the delay
private bool stop; //Stop for the shot delay (this section is in Update, its necessery so it doesnt over do the Reload)
private bool stop2 = false; //Other stop for the healing process(also in update section)
private bool healing = false; //Does it need healing (when hes below 30% of health)
//Create a delay for the shot
IEnumerator Reload(){
yield return new WaitForSeconds (reloadTime);
reloadNeeded = false;
stop = false;
}
//Set a new target
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 == taggedGameObjects[c].transform&&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>();
Health = MaxHealth;
Core = GameObject.Find("Core");
}
//Heal when the soldier is close to the core
IEnumerator Heal(){
while(Health<MaxHealth&&(transform.position - GameObject.FindGameObjectWithTag("Enemy").transform.position).sqrMagnitude < 20){
Health ++;
yield return new WaitForSeconds(0.3f);
}
stop2 = false;
}
//Destination setter
void SetDest(Transform destination){
agent.SetDestination(destination.position); //Set new destination
print(agent.destination); // print the agents current destination
}
//Damage receiver
public void Damage(int damage){
Health -= damage;
GameObject part = (GameObject)Instantiate(particle,transform.position,transform.rotation);
Destroy(part,1f);
}
void Update () {
//When its close to the core
if((transform.position - GameObject.FindGameObjectWithTag("Core").transform.position).sqrMagnitude < 20&&Health<MaxHealth&&stop2 == false){
StartCoroutine(Heal());
stop2 = true;
}
healing = true; // So it doesnt move to the target, and yes to the core, to prevent target-core conflicts
agent.SetDestination(GameObject.FindGameObjectWithTag("Core").transform.position); // set new destination, to the core
text.text = Health + "/" + MaxHealth;
text.gameObject.transform.rotation = Quaternion.LookRotation(Vector3.forward);
transform.position = new Vector3 (transform.position.x,1.0059f,transform.position.z);
GetNearestTaggedObject();
//stuff you dont need to know about
if(Health <= 0){
Destroy(gameObject);
}
if(target == true&&!healing){// if target is true and its not in state of almost death
if((transform.position - target.position).sqrMagnitude > stopOnDistance&&Health>0&&agent.destination != target.position){// if for other stuff
//Set destination number 1 (Working), for enemies
//SetDest(target);
}
//And other stuff ou dont need to know about
else if(Health>0)
agent.Stop(true);
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);
}
}
}
}
else
agent.Stop(false);
if (reloadNeeded&&stop == false) {
StartCoroutine(Reload());
stop = true;
}
}
}