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 Jimmyrrr · Nov 19, 2015 at 09:00 PM · c#optimizationmobile devicesmobileoptimization

Mobile Game AI Optimization Question

I am still pretty new with coding C# and Unity development, so I realize that some of the answers to my question might go over my head. However, I thought it would be a good idea to get some feedback from you guys before I went much further with my mobile game project.

As the title of my post indicates, my question is about optimizing the AI in my games so that it doesn't negatively impact the game's performance on mobile devices.

At the moment, this is the code that I am using for my AI:

  using UnityEngine;
     using System.Collections;
     
     public class BasicAI : MonoBehaviour {
         public Transform target;
         public int moveSpeed;
         public int rotationSpeed;
     
         private Transform myTransform;
     
         void Awake ()
         {
             myTransform = transform;
         }
     
     
         // Use this for initialization
         void Start () 
         {
             GameObject go = GameObject.FindGameObjectWithTag ("Player");
             target = go.transform;
         }
         
         // Update is called once per frame
         void Update () 
         {
             //Look at target
             myTransform.rotation = Quaternion.Slerp (myTransform.rotation, Quaternion.LookRotation                 (target.position - myTransform.position), rotationSpeed * Time.deltaTime);   
             
             //Move towards target
             myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
         }
     }

For anyone interested, this code came from BurgZurg's YouTube Channel: https://www.youtube.com/watch?v=O8-oZfi4utY&list=PLE5C2870574BF4B06∈dex=3

I would imagine that a single instance of this "AI" script would not cause any performance issues on mobile devices.

However, my current plan is to have 20-30 "Enemy units" all using this AI script at the same time, in order to target and attack the "Player." I have read that Unity's Update function is resource intensive, so I am concerned that if 20-30 instances of this "AI script" are all using the Update function simultaneously , that my game will suffer a huge performance hit. This is an especially big concern, given that this game will be running on mobile platforms.

This leads to several questions, which I hope that you guys can help answer:

  1. Should I be concerned about a performance hit while using so many instances of this script? Or do you think mobile devices will have enough processing power to make this a non-issue?

  2. Is there any way I can check to see how such a game would perform on mobile without a Unity Pro license?

  3. Is there a better way to set up an AI system in my game that would be less resource intensive?

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 NaRango4 · Nov 20, 2015 at 07:43 AM 0
Share

This is how frame rate changes for a typical game pipeline

graphics computation: materials, shaders

resources: textures, audio, media content, memory dependencies

finally Update() statement

If you check this famous plugin A* Path finding video. you will know what frame rates are.

1 Reply

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

Answer by tanoshimi · Nov 20, 2015 at 09:57 AM

It's good that you're thinking about the performance implications of your code early on, although I wouldn't necessarily say that there's any action you need to take at this point: your code already looks pretty lean - there's no unnecessary calculations being performed above the bare minimum if you want your units to move every frame (which I expect you do).

Also, while it's good to be mindful of efficient methods, "premature optimisation" is generally considered a bad thing in programming - you should concentrate on getting your code functioning right first, and only then worry about optimising it (if optimisation is even necessary). There's no point spending ages optimising code that turns out not to be a bottleneck or, worse still, ends up being cut from your project. To answer your specific questions:

  1. "Mobile devices" is a very generic term, encompassing everything from the first generation iPhone to the latest quadcore Android. You need to be more specific about considering what your target devices are when thinking about whether they have "enough" processing power. Having said that, nothing that you have written would be considered demanding for any modern device to handle.

  2. Of course. You can build and deploy the game to a mobile handset/tablet, and use the inbuilt Profiler to examine the resource and performance of your game exactly the same, whether you're using Unity Personal or Unity Pro.

  3. When you start to have more complex AI functions (such as, for example, pathfinding), you probably don't want to put these in Update(), since they will be recalculated every frame. Instead, learn about coroutines which can be scheduled to run less frequently (say, twice a second) - saving you unnecessary calculations, and allowing the possibility to split complex calculations up over several frames.

Comment
Add comment · Show 2 · 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 amanpu · Nov 20, 2015 at 05:22 PM 0
Share

(+1) for mentioning "premature optimization" .

avatar image Jimmyrrr · Nov 23, 2015 at 04:29 PM 0
Share

Thank you for you reply tanoshimi, you answered my question while simultaneously giving me some other things to think about.

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

33 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

Related Questions

Distribute terrain in zones 3 Answers

Does Unity support different pixel densities? 1 Answer

Multiple Cars not working 1 Answer

An OS design issue: File types associated with their appropriate programs 1 Answer

Problem with CrossPlaformInputManager.GetButton () 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