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

Character walks very slow in build

Discussion in '2D' started by salmonelson, Apr 6, 2020.

  1. salmonelson

    salmonelson

    Joined:
    Mar 16, 2020
    Posts:
    2
    I'm making a 2d game. For now I have made only walking character and some colliding stuff on a little map. I was just setting coordinates where my camera has to stop following on the edges of the map so I changed resolution to my computer's. And right after that when I built and ran the game my character started walking very slowly, although in editor it's still fine. I don't think resolution is a reason of my problem, changing it back didn't do anything. Though movement speed is very slow, animation speed didn't change. Changing character's speed didn't give any result.
     
  2. Aratow

    Aratow

    Joined:
    Nov 4, 2016
    Posts:
    49
    Is it only the character?
    Sounds like it has something to do with how you are calculating movement with deltaTime.
     
  3. salmonelson

    salmonelson

    Joined:
    Mar 16, 2020
    Posts:
    2
    Now the character is the only moving object.
    Here's whole movement script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     public float speed;
    8.     private Rigidbody2D myRigidbody;
    9.     private Vector2 change;
    10.     private Animator animator;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         animator = GetComponent<Animator>();
    16.         myRigidbody = GetComponent<Rigidbody2D>();
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         change = Vector2.zero;
    23.         change.x = Input.GetAxisRaw("Horizontal");
    24.         change.y = Input.GetAxisRaw("Vertical");
    25.         UpdateAnimationAndMove();
    26.     }
    27.  
    28.     void UpdateAnimationAndMove() {
    29.         if (change != Vector2.zero)
    30.         {
    31.             MoveCharacter();
    32.             animator.SetFloat("moveX", change.x);
    33.             animator.SetFloat("moveY", change.y);
    34.             animator.SetBool("moving", true);
    35.         }
    36.         else
    37.         {
    38.             animator.SetBool("moving", false);
    39.         }
    40.     }
    41.  
    42.     void MoveCharacter() {
    43.         myRigidbody.MovePosition(
    44.             new Vector2(transform.position.x, transform.position.y) + change * speed * Time.deltaTime);
    45.     }
    46. }
    47.  
    But I think deltaTime is alright, the problem occured suddenly and I didn't change anything in code. And it's fine in the editor.
     
  4. Aratow

    Aratow

    Joined:
    Nov 4, 2016
    Posts:
    49
    Weird. The code has no issues.
    I would suggest rebuilding your character again, starting with movement and test to see if it's still a thing, as inconvenient as it is to build the game for testing..

    The fact that it does that only on build make it seem like a bug.
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,511
    Are you running the physics per-frame? If not then I'm wondering why you're asking for the Rigidbody2D to move its position per-frame. As the docs state, Rigidbody2D.MovePosition will be actioned when the simulation runs which by default is the fixed-update at 50hz. If you had an FPS of 200Hz then you'd get 1 fixed-update per 4 frame renders. Given this scenario, you'd issue 4 MovePosition but only the last one would be used.

    Also, you're basing your position on Transform.position but the authoritative position when you use a Rigidbody2D is the body itself. After all, it's why it's there. You should use Rigidbody2D.position as the base position. This is even more important if you're using interpolation on the body as the Transform will always be behind the actual body position. Interpolation interpolates the Transform pose, not the body. The body is already at its final position where the simulation left it.

    This might not be the issue you're encountering but it is incorrect.
     
    sincerelysleepy likes this.
  6. sincerelysleepy

    sincerelysleepy

    Joined:
    Jan 15, 2019
    Posts:
    34

    I was having this same issue. I switched over to use the RigidBody position and it fixed the player moving slower after building. Thank you! Beat my face into this for hours.