- Home /
Android game lags when using transform.RotateAround to rotate an object with many children
I am making a 2D mobile game that features many different levels and I'm using sprite tilesets to keep Draw Calls low. There's a particular level that I want to make part of the walls rotating so I grouped all the walls tiles (each wall tile being a separate GameObject) inside an empty GameObject and applied a rotation script to the parent. It works fine on the editor but when I test the game on an Android phone it lags a lot. The FPS on the Android version goes down to 20 and, the game being an action game, renders that level unplayable. Each parent GameObject has about 80 children inside of it (because the walls are composed of a lot of tiles), and the level has 3 sets of rotating walls. If I disable some of the wall tiles inside the rotating parents, the FPS normalizes, so I think the problem is the CPU is having a rough time rotating all these children GameObjects, but I don't know how to fix it.
This is the code that I use to rotate the walls:
public class SpinAround : MonoBehaviour {
Vector3 center;
public float distance;
public float speed;
//defines direction of the rotation
public int sentido = 1;
Transform myTransform;
void Awake()
{
//defines the center of the rotation movement based on variable distance
myTransform = this.transform;
center = new Vector3(myTransform.position.x, myTransform.position.y, myTransform.position.z);
myTransform.position = myTransform.position + myTransform.up * distance;
}
void Update ()
{
transform.RotateAround(center, transform.forward, speed * sentido * Time.deltaTime);
}
}
Why is this happening? How can I make this rotating wall issue more optimized? Is RotateAround a heavy method? How could I avoid this lagging problem without changing the way I build levels (with many GameObjects composing the tiles of a wall)?
Thanks in advance for the help.
Are the game objects rigid bodies? Do they have colliders? I read in the manual that game objects with colliders but no rigidbody are considered "static", and are optimized as such, so rotating or moving them could cause performance issues. If it matches your situation, an easy fix is to add a rigid body component (with kinematic = true) on your wall tiles.
The objects have colliders but don't have rigidbodies. I've tried adding kinematic rigidbodies but the framerate got worse. Also, I don't know if this influences anything, but I'm using Collider2D and Rigidbody2D in this project.
Actually, maybe it's best to have one single rigidbody component on the wall ins$$anonymous$$d? Then all the children's colliders will be linked to that rigidbody. I don't know if the static collider issue applies to 2d, but it's worth a shot.
Also, have you confirmed that the framerate is better when the rotate line is commented?
Aside from that, and unless you can use the profiler in Unity pro, all I can think of is reducing the number of tiles :)
Yes, commenting out the rotate line normalizes the framerate.
Answer by khos85 · Jan 14, 2015 at 11:04 PM
Or have you tried moving: transform.RotateAround(center, transform.forward, speed sentido Time.deltaTime);
into FixedUpdate instead of Update Function, Update is called more often if understand correcly.
Hope that helps.
I've tried moving that function to a Fixed Update and the framerate got really worse. That really didn't work unfortunately.
Hmm, so does the whole game lag or only the object rotating?
Everything lags, the framerate goes down to about 20-15 fps.
Do you have a lot of tris/polygons in your scene that are in view of camera when rotating? can yu show me a stats windows in editor when running it in the editor?
I have about 160 Tris and 330 Verts in the in view of camera when rotating.
Answer by thirdstatestudio · Dec 22, 2015 at 01:09 PM
I have encountered the same problem and tried so many things to set it. Finally found out that OpenGLES3 is causing the lag. Go to player settings --> Android settings --> Other settings --> Unmark Auto graphics API and delete OpenGLES 3 from the list.
Your answer
Follow this Question
Related Questions
How Should I Get a List of Child Objects 2 Answers
Getting transform info after branch 1 Answer
Accessing children of instances vs children of original prefab 1 Answer
Rotation promlems past 90 and 270 3 Answers
Is there any way to use parenting as an organization tool without creating a Transform bond? 3 Answers