Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by losingisfun · Jun 02, 2016 at 02:15 PM · coroutinesupdate function

Character AI - Are Coroutines less heavy than using functions kept in update

I'm making some fairly basic AI functionality for a variety of entities in my 2D game. The AI itself is not very heavy, its fairly basic, alert status, proximity detector, random walking when idle, and non-visible health and attacking features.

I imagine in one scene I could have anywhere from up to twenty or even forty entities with individual AI. At which it may get a little bit clunky. I've tried two options, to make the AI function solely within Update and FixedUpdate, calling outside functions for specific actions, like turning, attacking, jumping. I've also tried making the same type of AI and keeping it almost entirely within a Coroutine. I found that a Coroutine was much easier, took less lines of code, and kept my code a bit more neat and tidy (also easier to read).

But my main concern is (Since I don't really know much about how C# reads), even though a coroutine is easier for me to use, is it more or less heavy on the game? If it is too minor to really matter, no problem, but I would be interested to know which would be better practice.

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 chariot · Jun 02, 2016 at 02:21 PM 0
Share

I prefer Coroutines for tasks that linked with time - Update and FixedUpdate based on frames, so FPS parameter will affect it, Coroutines and their WaitForSeconds works perfectly for tasks that need to wait some time, not frames.

avatar image Jessespike · Jun 02, 2016 at 05:42 PM 0
Share

I think each yield produces a tiny bit of garbage (9 bytes?), also running many coroutines at once can drive your framerate to the ground if you're not careful. Coroutines are very useful, especially for time driven sequences. As long as you're not going overboard and replacing every Update with a coroutine, and have 10000 coroutines running at once, then you should be fine.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by ninja_gear · Jun 02, 2016 at 05:52 PM

Code isn't any less cheaper then code. Its not that Coroutines are 'better' then Update. BUT! You can make Coroutines run less often then Update via WaitForSeconds().

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 NoseKills · Jun 02, 2016 at 06:55 PM -2
Share

That's correct. however :) ... sry

avatar image
0

Answer by ericbegue · Jun 02, 2016 at 08:25 PM

There is not much difference between Update and couroutines. In fact, Coroutines are called at the same frequency as Update. Performance-wise, they are equivalent.

Thought, there is a known issue with coroutine about memory allocation, but that's being fixed: https://issuetracker.unity3d.com/issues/coroutines-generate-garbage-in-movenext

Excepted for this specific issue, it does matter whether you are using Update or Coroutine.

About keeping your code neat and tidy, you might be interested in Behaviour Tree, which is a great AI tool to describe AI behaviour and could be a good alternative to coroutines.

I'm the author of Panda BT (www.pandabehaviour.com), it's a script based behaviour tree engine.

Using this tool, defining an AI consists of breaking down your logic into simple tasks (implemented as C# function) which are then assemble into an hierarchical structure (a tree) by the mean of a BT script, which defines how and when these tasks are executed.

Furthermore, the execution of the tree can be visualized at runtime, giving you detailed information about what the AI is currently doing, which is extremely valuable for debugging.

If you have question about using this tool, you're welcome on this thread.

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.

Follow this Question

Answers Answers and Comments

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

Related Questions

WaitForSeconds using Update? 1 Answer

Multiple coroutines starting from Update() freeze/crash Unity 0 Answers

Game randomly subtracts 2 lives when it is only supposed to subtract 1 1 Answer

coroutine in update method 1 Answer

count how many times a player has played 0 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