Can this script be better optimized?
Hello!
I wrote a simple script for enemy cars to follow the player. I also have other scripts on the enemy cars that control smart actions such as shooting, ramming etc...
When I look in the profiler I see that this script Update is taking over 70% resources. Is there any way I can make this script better optimized for mobile devices?
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class BOTAI : MonoBehaviour {
private Rigidbody rb;
public int AddForceAmount;
public int bothealth;
public Transform PlayerToLookAt1;
public Transform PlayerToLookAt;
private Text missileText;
Quaternion targetRotation;
// Use this for initialization
void Start ()
{
//don't forget to change AddForceAmount Random.Range in ProxFront.CS too!
AddForceAmount = Random.Range (1400, 2600);
PlayerPrefs.SetInt("AddForceAmount", AddForceAmount);
rb = GetComponent<Rigidbody>();
PlayerToLookAt = GameObject.FindWithTag("Player").transform;
PlayerPrefs.SetInt("CarID", 2);
missileText = GameObject.Find("missileText").GetComponent<Text>();
missileText.text = "Missiles:" + "" + PlayerPrefs.GetInt ("missileamount");
}
// Update is called once per frame
void Update ()
{
AddForceAmount = PlayerPrefs.GetInt ("AddForceAmount");
Vector3 PlayerToLookAtTransform = new Vector3( PlayerToLookAt.position.x, this.transform.position.y, PlayerToLookAt.position.z);
this.transform.LookAt (PlayerToLookAtTransform);
rb.AddForce (transform.forward * AddForceAmount);
}
}
Thanks in advance for your ideas!
Answer by Em3rgency · May 04, 2016 at 05:44 PM
You're not doing anything bad your code. Nothing really to optimize here.
However, you're using physics. You have a rigid body, and you're applying forces to it. And I imagine you probably have more than one game object running this same script with physics calculations? Physics, in general, are rather computationally heavy. And while it might be fine on a PC, a mobile device can definitely have issues dealing with it.
Your best option would be to remove all the rigid bodies and forces and just modify the transform directly. It won't look as fancy, but it will run much faster.
But how can I remove all rigid bodies if I still need collisions and shooting?
You can detect collisions in code. I believe one of the colliding objects has to be set as a trigger to fire off the correct collision event. Its a harder way to do it, as you would have to handle how the objects move or whatever else they do when collisions happen, but potentially you would save resources.
Either that, or make your game with less objects that have physics :) You are developing for mobile, after all.
Well I do have the standard car controller on my bot and I've locked it so it is always accelerating, but rotating the wheel colliders seems impossible. If I can't find how to rotate the wheel colliders I'll just limit the amount of enemies to 4 and perhaps dynamically spawn them. $$anonymous$$y Samsung Tab 3 starts lagging at 5 enemies so I think most devices would handle 4 fine. For example if the player had to kill 10 enemies for a mission I could spawn 3. Then after every kill I could add an enemy. This might even be better as this would mean longer gameplay per level and no frustration of too many enemies trying to kill you at a time.
Your answer
Follow this Question
Related Questions
How to replace space keyboard with a simple tap on Android? 0 Answers
A more optimized way to adjust duplicate integer values In a list? 0 Answers
Performance question: Is it better to Instantiate particle systems OR use same particle system? 1 Answer
Problems disabling an AI and giving control to the Player 0 Answers
Help! Values from previous Serialized List<> shows up again after a while using Coroutine 0 Answers