Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Lag when rotating gameobjects

Discussion in 'Editor & General Support' started by unity_pkyXOvmZUQ-zDA, Apr 15, 2019.

  1. unity_pkyXOvmZUQ-zDA

    unity_pkyXOvmZUQ-zDA

    Joined:
    Mar 16, 2019
    Posts:
    3
    Hi everyone! I am quite new to developing games in Unity (but also in general).
    I've recently almost just finished a game, but the only issue is the lag.
    So basically, my game is a huge maze, made up of smaller sub-mazes, and each of those rotates 90 degrees (on the Y axis, ofc) each x seconds (so the maze itself is ever-changing, kind of, until you find the escape).
    However, in-game, whenever the sub-mazes rotate (and they all do at the same time, except for the one the player is in) it kind of stutters and the game lags for one second, which is quite annoying when you re trying to find the escape.
    May I add, I rotate the sub-mazes by having all components (the walls, decorations, etc.) attached to the base/floor, which is what is actually rotating.

    I have tried adding rigidbodies and check on the interpolate setting.
    My question is: how can I get rid of the lag, and make it rotate smoothly (I mean without that one second lag)? I do not know for sure if delta time would help it, but even if it did, I wouldn't know how to change the code to include that.

    Here is the script section responsible for the rotation:
    Code (CSharp):
    1. IEnumerator Rotation()
    2.     {  while (true)
    3.         {
    4.             yield return new WaitForSeconds(time);
    5.             if (playerIsNotHere == true)
    6.  
    7.             {
    8.                
    9.                 transform.Rotate(0, 90f, 0, Space.Self);
    10.             }
    11.         }
    12.     }
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    1,462
    You should use the profile to determine what specific operation(s) are taking all the time. Otherwise you're just theorizing what the issue is. One thing worth mentioning is to make sure you're not doing a bunch of Debug.Log while you're rotating, as Debug.Log will slow things down a lot if you're writing to it every frame.
     
  3. unity_pkyXOvmZUQ-zDA

    unity_pkyXOvmZUQ-zDA

    Joined:
    Mar 16, 2019
    Posts:
    3
    thanks for the answer! I dont use the debug.log.
    I have checked the profiler, and whenever it rotates, I get something like this:

     
    Last edited: Apr 16, 2019
  4. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    1,462
    Do you have tons of dynamic (non-kinematic) rigidbodies in your scene that are getting moved when these objects rotate? The profiler results are telling you that whatever you're doing is very expensive from a physics standpoint. But it might indicate it's because you're not taking your rigidbodies into account. Generally, moving rigidbodies means timing the movement with the physics timestep, and using the rigidbody-specific movement methods. So, instead of transform.Rotate, you'd usually want Rigidbody.MoveRotation. Does the thing you're rotating have a kinematic rigidbody on it? Do a lot of your rigidbodies have interpolation turned on?
     
  5. unity_pkyXOvmZUQ-zDA

    unity_pkyXOvmZUQ-zDA

    Joined:
    Mar 16, 2019
    Posts:
    3
    the only non-kinematic rigidbody is the player (which is a sphere with its mesh turned off, it is kind of a first-person);
    I added kinematic rigidbodies to all of the sub-mazes bases (the things which are rotating) and replaced transform.Rotate with Rigidbody.MoveRotation, like this:
    m_EulerAngleVelocity = new Vector3(0, 90f, 0);


    Code (CSharp):
    1. if (playerIsNotHere == true)
    2.            {
    3.                 Quaternion deltaRotation = Quaternion.Euler(m_EulerAngleVelocity);// * Time.deltaTime);
    4.                 m_Rigidbody.MoveRotation(m_Rigidbody.rotation * deltaRotation);
    5.             }
    this is how I found it implemented on the documention. I noticed that by also doing the * Time.deltaTime it gets rid of the lag but it doesnt do a 90 degree rotation, and messes the maze up. I think that it does not properly work because I do the rotations inside a corutine, because I check
    if (playerIsNotHere == true)
    every x seconds (so once the player has left a specific sub-maze, that one can start rotating at the same time with the others).

    Initially I did not have any rigidbodies attached to the sub-mazes bases (which are the things rotating), cuz the colliders are the only things useful to me, but it had the lag anyway.