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:

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?

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.

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