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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Vehicle in builded game moves much slower, than in editor

Discussion in 'Physics' started by fomedev, Oct 29, 2015.

  1. fomedev

    fomedev

    Joined:
    Aug 28, 2015
    Posts:
    10
    I'm doing 2D car game. When player presses button, car moves right. The problem is that it always runs good in editor, but in builded PC/Mac versions car moves much slower. But sometimes I reload level and it moves good. But then I reload level and it drives slow again. I've tried to move car with addForce, addForceAtPosition and velocity. It is all the same result. Is there problem with input manager? There are other gameObject with script
    void Update () {
    if (Input.GetKey(KeyCode.D))
    {
    car.GetComponent<carBeh>().goRight();
    }
    }

    Any ideas what's the problem?
     
  2. VorpalSilence

    VorpalSilence

    Joined:
    Oct 22, 2015
    Posts:
    13
    Try putting your code into the FixedUpdate() function rather than the Update() function. Update can have varying timing depending on frame-rate whereas fixedupdate will be consistently timed and should thus be consistent across your build platforms.
     
  3. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    816
    First, do all physics related stuff (like moving) in the fixed update function as Vorpal Silence suggests. Also always keep in mind, that on performane peeks the fixed update won't be every fixed time slot, so multiply Time.fixedDeltaTime to every variable necessary. So for example:
    Code (CSharp):
    1. // Don't do it like this
    2. transform.position += transform.forward * 0.1f;
    3.  
    4. // Do it like this
    5. transform.position += transform.forward * 6 * Time.fixedDeltaTime;
    For rigidbody.AddForce it is not necessary by the way.