Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
This question was closed Mar 06, 2014 at 08:28 AM by Fattie for the following reason:

Duplicate Question

avatar image
1
Question by Chona1171 · Mar 06, 2014 at 05:49 AM · aimono-behaviour

AI Optimization

Hi to all

There has been something on my mind for a while now , what would be better. When I have an AI script for lets say enemies , that does the distance raycast calculation and the movement and I a. Assign it to each individual enemy b. Have a game engine script that basically does the same but on the update command loop through each enemy in an array and does the required updates ?

My common sense would point me to option b as there would be far less declarations of the AI script but heres the thing , what if the AI enemies become a huge amount , would the thread it runs on be slowed down as apposed to when each enemy gets their own behavior script and each run on their own thread ?

Comment
Add comment · Show 2
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 fifthknotch · Mar 06, 2014 at 06:02 AM 0
Share

I would definitely go with option A. Update functions have little processing overhead (depending on how big the code inside the update is of course) but the real reason is control. If each update is called on each enemy, you have more control over what each enemy does. Also, by throwing in a few if statements, you could bypass the update altogether (if you want to cut back on calculations). Otherwise, with option B you will have to go through lines of code, calculations, and if statements for each enemy - one at a time, As you noted, this could slow down the enemies' update process so some would track the player while others lagged behind (this would require a LOT of enemies for this to actually happen)

avatar image Fattie · Mar 06, 2014 at 08:28 AM 0
Share

This is just a general program$$anonymous$$g question. The simple answer is to test both in your situation.

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by fafase · Mar 06, 2014 at 05:55 AM

The best way is to test.

I would also think answer b gives better optimization as you only get one Update call and it runs a for loop which is no issue.

As for your concern on would it slow down the thread, the only difference is that instead of doing the same thing in many places it does it many times in the same place.

Again, you would have to test since when running on the script you get direct access to the members while running on a separate script you need a dereferenciation which is no big deal though.

Your idea of less script and better memory management is wrong. You would save the Update method but the variables should still be in separate scripts. Or it means you have a large array or list but then you still use pretty much the same amount (you would save the overhead of the object).

Finally, if you think this will run slow when too many guys are in, you can minimize the amount of computation using a timer or InvokeRepeating. This way you can get your Update to run only at specific frequency.

 void Start){
    // First update after 0.05s and every 0.5s 
    InvokeRepeating("UpdateMethod",0.05f,0.05f);
 }  
 
 void UpdateMethod(){
    // Here is the update
 }

You can also cut your AI using coroutines so that you do some computation for a certain time and come back next frame to finish it off.

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
avatar image
0

Answer by whydoidoit · Mar 06, 2014 at 05:55 AM

Well its what you prefer really - I prefer my NPCs to be autonomous and have their own scripts. The instance of the script is effectively just the size of the variables that define its current state (plus a small overhead) and so it will make no material difference.

Next there are no threads for this in Unity. You can have your AI run on a separate thread if you write it that way (see http://unitygems.com/threads) but it has to not interact with any scene objects at the time, so raycasting is out.

Your best bet is to use coroutines and yield to have AI run across several frames - so you should separate your movement from your decision making allowing the movement every frame but the decision making to be spread. This will allow you to have much more intelligence by (for example) using more raycasts as feelers around dynamic obstacles etc.

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

Follow this Question

Answers Answers and Comments

23 People are following this question.

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

Related Questions

Controlling AI Movement 0 Answers

a Script for Enemy HP 1 Answer

Forward movement Slow? Delta.time? Frame Rate? 0 Answers

Adding Object Avoidance to my Enemy AI Script 1 Answer

Simple AI Script help 2 Answers


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