How can I prevent my While Loop targeting system from producing a massive framedrop?

Basically, I am creating a game where you fire projectiles from one planet to another. So, I have the projectile system created and working fine, with a targeting “preview line”. However, when I apply the same code to the enemy, with a random velocity selector, so it can figure out what angle and velocity to shoot so the projectile hits my planet. Basically, a targeting system for an AI.

	tarx = 0;
	tarz = 0;
	var nx=-25f;
	while (nx<25f){
		var nz =-25f;

		while (nz<25f){
		 	
			var gravity = Vector3(GameObject.Find("Sun").transform.position.x,0,GameObject.Find("Sun").transform.position.y)-Vector3(transform.position.x,0,transform.position.z);
		    var position = transform.position;
		    var velocity = Vector3(nx,0,nz);
		    //var timeDelta = 1.0f / velocity.magnitude;
		    var timeDelta = Time.deltaTime;
		    var numSteps = 400; // for example
		    var i=0;
		    //Debug.Log(Vector3(nx,0,nz));
		    while (i < numSteps)
		    {
		 
		        position += 0.25*velocity * timeDelta + 0.5f * gravity * timeDelta * timeDelta;
		        //position = velocity*Time.deltaTime + 0.5f*gravity*Time.deltaTime*Time.deltaTime;
		        velocity += 0.25*gravity * timeDelta;
		        
		        if (i>35 && Vector3.Distance(transform.position,position) < 2){
		        i=numSteps;
		        break;
		        }
		        
		        if (Vector3.Distance(enemy,position) < 0.3){
		        Debug.Log("Enemy Targeting Soltuion Found!");
		        tarx = nx;
		        tarz = nz;
		        i=numSteps;
		        // FIRE PROJECTILE CODE HERE
		        break;
		        }
		        
		        i+=1;
		    }
		    if (tarx !=  0){
		    nz = 55f;
		    break;
		    }
			nz+=0.5f;
		}
		if (tarx !=  0){
			nx = 55f;
		    break;
		    
		    }
			nx+=0.5f;
	}

I know why it is causing the massive framedrop everytime it tries to find a target, every shot it is taking 4000000 different variations into account. I have tried to lower the amount it does by stopping it when it has identified a target. However, it still causes a bad framedrop. Does anyone have any suggestions?

Thanks for listening.

You should AT LEAST not do a GameObject.Find inside the loops. You’re searching through every object in your game to find the sun object 2500 times. That is 2500 * (number of game objects) string comparisons.

Wait, you’re doing Find twice, so it’s 5000 searches you don’t need.

Do this instead:

var sunPosition = GameObject.Find("Sun").transform.position;
var position = transform.position;
var gravity = Vector3(sunPosition.x,0,sunPosition.y) -Vector3(position.x, 0, position.z);

var nx=-25f;
while (nx<25f) {

    var nz =-25f;
    while (nz<25f) {
        var velocity = Vector3(nx,0,nz);
        ....

That should improve things a lot. In general, never use GameObject.Find, or GameObject.FindWithTag - they require you to search through everything in your scene. Doing them inside a loop is a framerate felony.

In general, everything inside a loop that’s not dependent on other things in the loop should happen outside the loop.