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.