- Home /
AI optimization
Hi.
An AI is a very broad topic, but I would like to know specifically how AI in games do not tear the framerate a new hole to breath through.
In my AI I usually use Vector3.Distance to tell the AI when to attack and what not when it is within a specifically range. But measuring distance on several AIs are very recourse expensive and tears the frame rate to pieces. But in games like Halo, Battlefield, COD there are sometimes 20 plus enemy AIs and plus friendly ones, but how do they manage to have a steady and solid framerate with that many AIs?
But just creating a game in general is such a huge -blaming- challenge. But I find AI very difficultly. I know there are several topics on how to optimize the framerate, but I am more about creating a AI with low loss of framerate using different techniques.
What are some no-no´s when scripting an AI? In order for them to be "clever" a lot of code is required. Therefor more code = less framerate.
I guess Vector3.Distance is a no-go. An alternative is triggers? or other?
But what about Instantiate? The AI Instantiates bullets(In Halo needles, plasma bullets that the player can actually see and get hit by).
Raycasting? What determines whether the player gets hit by a bullet fired from the enemy AI? Raycasting is expansive when used in large numbers.
I am sorry of this is to much, but I am on very deep water here. My technique for creating an AI is usually expensive and using one or more of the things above and I would like to hear an alternative to those three tings above. Some less expensive for creating several AIs. And if I left any out please tell me.
Thank you so much.
Very nice read. Some alternative to Raycasting could be nice, but I just thought of and large trigger in the forward direction of the player and check if an enemy or whatnot is in it and do stuff accordingly.
Answer by Owen-Reynolds · Jul 16, 2011 at 04:01 PM
Main trick is to run AI maybe 2 times/second. With a framerate of 50, that's a 25 times speed-up right there. It also looks better, since the enemy doesn't snap-respond to you: if you pop out from behind a wall, the orc takes 0-1/2 sec to see you, depending when it last ran the code. If you decide to move left around that tree, you are committed for 1/2-sec, so won't ping-pong as much. Stagger the times for each enemy, so one frame doesn't get swamped, and you get the bonus that now a group of orcs "randomly" sees you (instead of snapping all a once to chase you.)
You have to be a little careful to, say, make sure your "am I close enough to waypoint" checks have a target zone you can't overshoot in 1/2-sec.
Code (from memory):
public float AI_DELAY = 0.5f;
void Start() {
...
StartCoroutine(runAI());
...
}
void runAI() {
// we all random wait, so our AI frames are staggered:
yield return new WaitForSeconds(Random.value * AI_DELAY);
while(true) {
... run AI code, raycasts, etc...
// optional playing with delay:
float delay = AI_DELAY;
if( farFromPlayer ) delay *= 2;
else if (nearPlayer) delay /=2;
yield return new WaitForSeconds(delay);
} // AI loop
}
Your answer
Follow this Question
Related Questions
Raycasting 1 Answer
How to force static batching being performed at runtime? 0 Answers
Physics.RayCast problem with instantiated prefab 0 Answers
Confirm Instantiation or Object Confirm it's Instance 2 Answers