• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by FLASHDENMARK · Jul 16, 2011 at 03:38 PM · instantiateaioptimizationraycasting

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LegionIsTaken · Jul 16, 2011 at 03:44 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Finjitzu · Jan 25, 2012 at 06:08 AM 0
Share

Good answer! Gonna try this tomorrow

avatar image
0

Answer by super3 · Feb 15, 2012 at 09:01 AM

Very cool ,just like my idea;)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Checking if object intersects? 1 Answer

Can my script instantiate an object then get that object for use in the script 1 Answer

Another Raycast Issue. 0 Answers

Make AI walk on instantiated platforms ? 0 Answers

Unlimited Blood on Floor 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges