- Home /
How to cycle Vector3 transformations correclty perfomance-wise?
Just learned that multiple Vector3 transforms in cycle on Update can hit performance very bad. Is there any other way to move objects around instead of Vector3.
Need some context about what you are trying to do: Learned from where? Are we talking about a dozen, hundreds, thousands? What platform? Are you caching your transform locally? Do you have some specific code we can look at?
Exactly, what kind of transformations? Also your last sentence makes no sense. You don't "use" a Vector3 to move an object. The Vector3 describes the position of the object. So when it should change it's position, you have to change the Vector3.
Please add more details because this question isn't specific at all. Otherwise we have to close the question.
To clarify things imagine this
for(int i =0; i < arrayGameObject.Length; i++){
cachedTransform = arrayGameObject[i].transform;
//multiple cachedTransform.position, cachedTransform.rotation calls,
//Vector3 transformations
}
Where arrayGameObject is GameObject[] and can be about 500+ size
To clarify things imagine this
for(int i =0; i < arrayGameObject.Length; i++){
cachedTransform = arrayGameObject[i].transform;
//multiple cachedTransform.position, cachedTransform.rotation calls,
//Vector3 transformations
}
Where arrayGameObject is GameObject[] and can be about 500+ size
Answer by robertbu · Jul 23, 2013 at 05:52 PM
The issue here is unlikely to be related to the transform. You can make your code a bit more efficient by using an array of Transforms rather than an array of GameObjects. You can always get to the game object using Transform.gameObject. I've read that under the hood, Unity does a GetComponent() call to get the Transform, so it is not just the indirection that is a potential slowdown.
But with that said, this code isn't that slow. If you want to test it, populate your list with empty game objects or with game object with their renderer disabled. Run your movement code, increasing the size of your array until you see a FPS drop. My guess is that you will hit the wall with memory before this code becomes and issue, and even 10,000 empty or non-rendered objects will not slow down the FPS. But creating that many object will cause a slowdown, so take any readings while objects are not being created. If you are concerned, run the test. Make sure you run it in a build rather than in the editor, since there are slowdowns in the editor due to large numbers of objects that don't occur in a real build. And you can use this script to show FPS:
http://wiki.unity3d.com/index.php?title=FramesPerSecond
I predict that any issue with 500 game objects will have a lot more to do with rendering than how you move them. Performance will depend mesh complexity, how the game objects batch and the efficiency of the shader. More info on batching:
http://docs.unity3d.com/Documentation/Manual/DrawCallBatching.html
Array of Transforms ins$$anonymous$$d of GameObjects helped a lot!
Also helps reducing timeScale and polycount of models
And i hope to optimize code for cycle. for now its feasible 35 fps per 300 units on A7 device
As you can guess from my answer, I'm surprised that using Transforms rather than GameObjects made a substantial difference. I expected some, but not as much as "helped a lot!"
Just to add onto this, you could also add it into a coroutine and only do X amount of transforms a frame.