- Home /
More than 20 instances of a certain script cause fps to skyrocket, is there something I'm doing wrong here?
Hi, I'm currently working on the AI for an RTS game I'm working on. One of the AI's is set to find the nearest target, move to it, then destroy it, then repeat. If it gets shot in this cycle however, it finds the nearest shooter, kills it, then continues to the original cycle. The AI works as intended, but not to any considerable scale. More than 20 of these AIs spawned results in a severe fps drop (not a problem for any other scripts, and I need to be able to handle up to 200. Here is the AI code, I'm not sure if there is any one issue, or if the whole script needs to be rewritten.
public class activeSoldierAI : MonoBehaviour { GameObject target; Target targettype; bool allclear = true; int hp = 50; int lastsavedhp; bool isShooting; Rigidbody selfBody; // Use this for initialization void Start () { targettype = Target.Building; target = ClosestTarget ("Building"); lastsavedhp = hp; StartCoroutine (shoot()); selfBody = GetComponent ();
}
// Update is called once per frame
void Update () {
Debug.Log (hp);
Debug.Log (allclear);
//Debug.Log (targettype);
Vector3 forward = transform.TransformDirection(Vector3.forward) * 10;
Debug.DrawRay(new Vector3(transform.position.x,transform.position.y + 1.5f,transform.position.z), transform.forward, Color.green, 15000);
if(targettype == Target.Building){
if(!Physics.Raycast(new Vector3(transform.position.x,transform.position.y + 1.5f,transform.position.z), transform.forward, 25) && target != null){
transform.LookAt (new Vector3(target.transform.position.x, transform.position.y, target.transform.position.z));
rigidbody.AddForce (transform.forward * 7 * 60 * Time.deltaTime);
isShooting = false;
selfBody.drag = 0;
}else if(target != null){
isShooting = true;
selfBody.drag = 6;
}else{
target = ClosestTarget("Building");
//Debug.Log("HOwde doo");
}
}
if (hp < lastsavedhp && allclear == true) {
targettype = Target.Soldier;
target = ClosestTarget ("reserveSoldier");
allclear = false;
}
if(targettype == Target.Soldier){
if(target != null){
Debug.Log("YUR FUCKED MATE");
isShooting = true;
transform.LookAt (new Vector3(target.transform.position.x, transform.position.y, target.transform.position.z));
selfBody.drag = 6;
}else{
targettype = Target.Building;
target = ClosestTarget("Building");
isShooting = false;
allclear = true;
lastsavedhp = hp;
Debug.Log("HOwde doo");
}
}
if (hp <= 0) {
Destroy(gameObject);
}
}
IEnumerator shoot() {
while (true) {
if(isShooting == true){
if(targettype == Target.Building){
target.GetComponent<buildinghealth>().TakeDamage();
Debug.Log("Blammo!");
}else{
target.GetComponent<reserveSoldierAI>().TakeDamage();
Debug.Log("Blammo!");
}
//Debug.Log("Blammo!");
}
yield return new WaitForSeconds(1f);
}
}
public void TakeDamage(){
hp -= 10;
}
GameObject ClosestTarget(string tag){
GameObject[] objectsWithTag = GameObject.FindGameObjectsWithTag (tag);
GameObject closestObject = null;
foreach (GameObject obj in objectsWithTag) {
if (!closestObject) {
closestObject = obj;
}
if (Vector3.Distance (transform.position, obj.transform.position) <= Vector3.Distance (transform.position, closestObject.transform.position)) {
closestObject = obj;
}
}
return closestObject;
}
}
First of all remove all debugging tools! These lag the game alot! Also ins$$anonymous$$d of having everything in your update function you should try to use threads and coroutines with a quick timer. Also use return statements where necessary. If this doesnt work youre gonna have to come up with something much more clever and efficient I am afraid :/
Everything inside the update function must be completed before the next frame can start rendering, but with coroutines the game can render several frames without having to stop, atleast if you use yields correctly!
The biggest problem is probably those Debug.Log calls that run every frame. Those would be a big problem when running in the editor.
The more common way to write your Q is "script runs too slow."It's like buying a $25 cup of coffee. Once, not a huge deal. 20 times a week, it adds up. But the root cause is the script/coffee costing too much.
For example, there are are few Qs here, I think, about faster ways to find the nearest enemy.
Your answer
Follow this Question
Related Questions
Calling gameobject.transform vs. just calling transform directly - Performance negligible? 1 Answer
Figuring out which specific objects are slow to render? 0 Answers
How to implement Jobs?,How can I increase performance with Jobs? 0 Answers
Problem with the performance when activating gameObject 0 Answers