- Home /
How can I optimize this script?
I need to optimize this script, because when I move the object it uses nearly 40% of my CPU (You can see it in the screenshot) and it reduces the FPS from 80 to 40.
Here is my code:
using UnityEngine;
using System.Collections;
public class JointFree : MonoBehaviour
{
private GameObject telega;
private GameObject most;
// Use this for initialization
void Start ()
{
telega = GameObject.Find ("cran_i_cabina/most/telega");
most = GameObject.Find ("cran_i_cabina/most");
}
// Update is called once per frame
void Update ()
{
var position = gameObject.transform.position;
var rotation = gameObject.transform.eulerAngles;
Debug.Log (transform.eulerAngles.x > 0.0001f || transform.eulerAngles.z > 0.0001f);
if (Mathf.Abs(transform.eulerAngles.x) > 0.000001f || Mathf.Abs(transform.eulerAngles.z) > 0.000001f) {
gameObject.transform.eulerAngles = new Vector3 (0.0f, rotation.y, 0.0f);
}
if (Mathf.Abs(telega.gameObject.transform.position.x - position.x + 0.103f) > 0.0001f || Mathf.Abs(telega.gameObject.transform.position.y - 10.86f) > 0.0001f || Mathf.Abs(telega.gameObject.transform.position.z - position.z + 0.11f) > 0.0001f) {
telega.gameObject.transform.position = new Vector3 (position.x - 0.103f, 10.86f, position.z - 0.11f);
}
if (Mathf.Abs(telega.transform.eulerAngles.x) > 0.0001f || Mathf.Abs(telega.transform.eulerAngles.y) > 0.0001f || Mathf.Abs(telega.transform.eulerAngles.z) > 0.0001f) {
telega.gameObject.transform.eulerAngles = Vector3.zero;
}
if (Mathf.Abs(most.gameObject.transform.position.x - position.x - 2.133f) > 0.0001f || Mathf.Abs(most.gameObject.transform.position.y - 10.8535f) > 0.0001f || Mathf.Abs(most.gameObject.transform.position.z) > 0.0001f) {
most.gameObject.transform.position = new Vector3 (position.x + 2.133f, 10.8535f, 0.0f);
}
if (Mathf.Abs(most.transform.eulerAngles.x) > 0.0001f || Mathf.Abs(most.transform.eulerAngles.y) > 0.0001f || Mathf.Abs(most.transform.eulerAngles.z) > 0.0001f) {
most.gameObject.transform.eulerAngles = Vector3.zero;
}
}
}
Answer by Dave-Carlile · Nov 08, 2013 at 03:47 PM
Remove the call to Debug.Log
. Executing those every update is bad.
Thank You. It helped a little bit and now FPS reduces from 80fps to 55 fps.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How do I efficiently maintain a constantly-changing list without triggering GC? 2 Answers
Programatically driven particles 1 Answer
The nested type `TransformDirection' does not exist in the type `UnityEngine.Transform' 1 Answer
Set controller to active script - how do I transform him to the correct position? 1 Answer