Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Why Does My Build Slow Time?

Discussion in 'Editor & General Support' started by Marshodactyl, Apr 24, 2022.

  1. Marshodactyl

    Marshodactyl

    Joined:
    Apr 24, 2022
    Posts:
    2
    I am new to Unity, and I was creating a small nothing project just so I can learn the basics and for fun. However, when I play a build of my project, time slows down.

    Recording of the editor:

    Recording of the build:


    I do not know why everything involving the Update() method works slower in the build, and I'm hoping I can get a solution. As a side note, I also don't know why the lighting is darker. It happens when I load the level from my main menu in both the build and editor, but doesn't when I load the level directly from the editor.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,964
  3. Marshodactyl

    Marshodactyl

    Joined:
    Apr 24, 2022
    Posts:
    2

    1. I'm multiplying the rotation speed of my objects (excluding my character) by Time.timeScale (see first code). If I instead multiply by Time.deltaTime, they don't rotate at all. For my character, I have an integer tracking what angle he should be facing which is either increased or decreased if he should turn either clockwise or counterclockwise. The character's y rotation is then set to this integer when it changes (see second code).

    Code (CSharp):
    1. public class CoinRotation : MonoBehaviour
    2. {
    3.  
    4.     public int rotateSpeed = 1;
    5.  
    6.     // Update is called once per frame
    7.     void Update()
    8.     {
    9.        transform.Rotate(0,rotateSpeed * Time.timeScale,0,Space.World);
    10.     }
    11. }
    The last line is what sets the rotation. Everything else is what determines the angle.
    Code (CSharp):
    1. void turnTo(int ang)
    2.     {
    3.         ang = ang % 360;
    4.  
    5.         if((ang - playerAngle >= -360 && ang - playerAngle < -180) || (ang - playerAngle >= 0 && ang - playerAngle < 180) && ang != playerAngle)
    6.         {
    7.             //If shortest distance is to rotate clockwise... Add turnSpeed
    8.             playerAngle = (playerAngle + playerTurnSpeed) % 360;
    9.         }else{
    10.             //If shortest distance is to rotate counterclockwise... Subtract turnSpeed
    11.             playerAngle = (playerAngle - playerTurnSpeed) % 360;
    12.         }
    13.         if((ang - playerAngle + 360) % 360 <= playerTurnSpeed){
    14.             //Within turnSpeed distance of ang
    15.             playerAngle = ang;
    16.         }
    17.         playerAngle = (playerAngle + 360) % 360;
    18.         playerMario.transform.localRotation = Quaternion.Euler(0,playerAngle,0);
    19.     }
    Also, here's a small snippet of what's inside Update() for my player's rotation just in case it's relevant. It follows the same pattern for all 8 directions.
    Code (CSharp):
    1. if(Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.A))
    2.         {
    3.             turnTo(45);
    4.         }
    5.         else if(Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.A))
    6.         {
    7.             turnTo(135);
    8.         }
    2. I have no calls to FixedUpdate() anywhere in my program, and before you mentioned it, I didn't know it existed. From reading what articles you've sent I've gathered that FixedUpdate() continues calling at a stagnant rate, disregarding framerate and lag, correct?

    3. When I change my Update() functions to FixedUpdate(), the objects rotate like how they do when I previously created a build, slower. Is there any problem with using Update() for constant object transformation like coin rotation that's causing them to slow in a build?

    4. Something that actually DOES move at the correct speed in the build is my character's movement speed. What is it about this code that lets movement speed work as intended?

    Inside Update()
    Code (CSharp):
    1. if(Input.GetKey(KeyCode.A))
    2.         {
    3.             transform.Translate(Vector3.left * Time.deltaTime * moveSpeed);
    4.         }
    5.         if(Input.GetKey(KeyCode.D))
    6.         {
    7.             transform.Translate(Vector3.left * Time.deltaTime * moveSpeed * -1);
    8.         }
    9.         if(Input.GetKey(KeyCode.W))
    10.         {
    11.             transform.Translate(Vector3.forward * Time.deltaTime * moveSpeed);
    12.         }
    13.         if(Input.GetKey(KeyCode.S))
    14.         {
    15.             transform.Translate(Vector3.forward * Time.deltaTime * moveSpeed * -1);
    16.         }
    Finally, thank you for attempting to help me out! I hope my response wasn't hard to follow or confusing at all.