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. Dismiss Notice

Build works faster than Editor

Discussion in '2D' started by Trekko, Oct 21, 2020.

  1. Trekko

    Trekko

    Joined:
    Jan 24, 2018
    Posts:
    21
    Hi
    Few days ago I decided to start making project in Unity 2D instead of 3D. and today I have first problem.
    My player object was moving very slow so I decided to increase it but after build game i also saw that builded game works many times faster than in editor and now i have to set speed from scrath. How to fix it?
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
  3. Trekko

    Trekko

    Joined:
    Jan 24, 2018
    Posts:
    21
  4. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
  5. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    "My player object was moving very slow so I decided to increase it but after build game i also saw that builded game works many times faster than in editor and now i have to set speed from scrath."

    The question is very vague... how have you actually setup you movement? If I have to guess, that sounds like you haven't used delta time at all for your movement while moving objects, and then you get more frames, thus your movement is faster in your build with higher framerate.

    "I decided to start making project in Unity 2D instead of 3D"

    There is no separate Unity 2D or Unity 3D. Do you perhaps mean you are using Physics2D for your dynamic objects?
     
  6. Trekko

    Trekko

    Joined:
    Jan 24, 2018
    Posts:
    21
    Here is my code :
    Code (CSharp):
    1.  
    2. private void Update(){
    3. GetInput();
    4. }
    5.     public void GetInput()
    6.     {
    7.         if (Input.GetAxis("Vertical") > 0.1f || Input.GetAxis("Vertical") < -0.1f)
    8.             verticalSpeed = Input.GetAxis("Vertical");
    9.         else
    10.             verticalSpeed = 0;
    11.         if (Input.GetAxis("Horizontal") > 0.1f || Input.GetAxis("Horizontal") < -0.1f)
    12.             horizontalSpeed = Input.GetAxis("Horizontal");
    13.         else horizontalSpeed = 0;
    14.         Move();
    15.     }
    16.     public void Move()
    17.     {
    18.         rb.velocity = new Vector2(horizontalSpeed, verticalSpeed)* acc * Time.deltaTime;
    19.     }
     
  7. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    When using the physics system, movement should be done in FixedUpdate. Moving your Rigidbody2D in Update is likely what's causing the problem.

    Try this:
    Code (CSharp):
    1.  
    2.     Vector2 movement;
    3.  
    4.     private void Update()
    5.     {
    6.         GetInput();
    7.     }
    8.  
    9.     private void FixedUpdate()
    10.     {
    11.         Move();
    12.     }
    13.  
    14.     public void GetInput()
    15.     {
    16.         float xInput = Input.GetAxis("Horizontal");
    17.         float yInput = Input.GetAxis("Vertical");
    18.  
    19.         if(xInput < 0.1f && xInput > -0.1f)
    20.         {
    21.           xInput = 0;
    22.         }
    23.         if(yInput < 0.1f && yInput > -0.1f)
    24.         {
    25.           yInput = 0;
    26.         }
    27.  
    28.         movement = new Vector2(xInput, yInput);
    29.     }
    30.  
    31.     public void Move()
    32.     {
    33.         rb.velocity = movement * acc * Time.fixedDeltaTime;
    34.     }
     
  8. Trekko

    Trekko

    Joined:
    Jan 24, 2018
    Posts:
    21
    Problem is now partially resolved because I cannot move player in Editor and builded game xD
     
  9. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @Trekko

    If you want to convert your input to directional movement, get the input (hor/vert as vector2) and then multiply it with your speed.

    You might also need to clamp/normalize it, depending on what you want.

    Don't use deltaTime when setting velocity, you are not doing per frame change of velocity, you are setting it to a certain velocity. I don't know what that acc (acceleration?) is doing there.

    Velocity in Unity is units per second.
     
  10. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Just tested it on my end and it works fine.
    Perhaps your
    acc
    value is too low?
     
  11. Trekko

    Trekko

    Joined:
    Jan 24, 2018
    Posts:
    21
    Yes you're right. I forgot to set it. Now everything works same in the editor and in build <3 Thank you
     
  12. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @Trekko

    I bet you didn't even read my answer... but anyway... by adding acceleration, you only introduce some magic number into your velocity. Let alone using Time.fixedDeltaTime there. It doesn't do anything meaningful, but makes it harder to set your velocity to some exact units/second speed.
     
  13. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    @eses is also correct.
    Typically you don't move a Rigidbody/2D by setting it's velocity directly, rather you would use AddForce for dynamic bodies or MovePosition for kinematic bodies.
     
  14. Trekko

    Trekko

    Joined:
    Jan 24, 2018
    Posts:
    21
    I bet he's but after 3 years of learn I'm still noob and when I saw on YouTube that it's works I start to use it without research.