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 /
  • Help Room /
avatar image
0
Question by DroidifyDevs · May 04, 2016 at 03:56 PM · c#androidscripting problemaioptimization

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!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 3 · 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 DroidifyDevs · May 04, 2016 at 07:27 PM 0
Share

But how can I remove all rigid bodies if I still need collisions and shooting?

avatar image Em3rgency · May 05, 2016 at 11:16 AM 0
Share

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.

avatar image DroidifyDevs Em3rgency · May 05, 2016 at 08:56 PM 0
Share

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

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

175 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 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 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 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

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


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