Search Unity

[FIXED] Lags on a camera movement (or a player movenent)

Discussion in '2D' started by Oleksii_Serheiev, Aug 8, 2019.

  1. Oleksii_Serheiev

    Oleksii_Serheiev

    Joined:
    Mar 7, 2015
    Posts:
    9
    Hello All,

    Unfortunately, I have faced an issue that looks quite tricky for a beginner.
    When a player moves along the scene It seems a playground periodically lags.
    I guess the root cause is related to a player movement approach or to a camera that follows the player.

    The cinemachine is used as a camera.
    Please, take a look below for the camera settings and the movement script:
    upload_2019-8-8_22-39-59.png

    upload_2019-8-8_22-41-16.png

    Player's RigidBody2D:
    upload_2019-8-8_22-43-40.png

    Code (CSharp):
    1. public class PlayerController : MonoBehaviour
    2. {
    3.     [SerializeField]
    4.     private float speed = 1;
    5.     [SerializeField]
    6.     private Vector2 lookDirection = new Vector2(1, 0);
    7.  
    8.     private Rigidbody2D rigidbody2d;
    9.     private Animator animator;
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.         rigidbody2d = GetComponent<Rigidbody2D>();
    15.         animator = GetComponent<Animator>();
    16.     }
    17.  
    18.     void FixedUpdate()
    19.     {
    20.         float horizontalMovement = Input.GetAxis("Horizontal");
    21.         Vector2 movement = new Vector2(horizontalMovement, 0.0f);
    22.         Vector2 position = rigidbody2d.position + movement * speed * Time.fixedDeltaTime;
    23.  
    24.         if (!Mathf.Approximately(horizontalMovement, 0.0f))
    25.         {
    26.             lookDirection.x = horizontalMovement;
    27.             lookDirection.Normalize();
    28.         }
    29.  
    30.         animator.SetFloat("Speed", movement.magnitude);
    31.         animator.SetFloat("Direction", lookDirection.x);
    32.  
    33.         rigidbody2d.MovePosition(position);
    34.     }
    35. }
    36.  
    PS: I have tried to google the issue and saw a lot of topics but neither of them has helped.

    Thanks All in advance.

    SOLUTION:
    The root cause was that I have not tried to build and run with enabled Interpolation.
    Due to I have enabled Interpolation and tried it only with Unity emulator It produced jerking and I have thought Interpolation has not fixed the issue.

    So that, Guys, I would recommend trying to build with enabled Interpolation and run your game.
     

    Attached Files:

    Last edited: Sep 19, 2019
  2. orchard800

    orchard800

    Joined:
    Jul 28, 2015
    Posts:
    19
    Is your camera movement in LateUpdate rather than Update?
     
  3. Oleksii_Serheiev

    Oleksii_Serheiev

    Joined:
    Mar 7, 2015
    Posts:
    9
    Hi orchard800,

    There is a screenshot with the camera settings above.
    The camera update is set to Smart Update.
    I have tried to change it to Late Update and Fixed Update but it produces more stutters.
     
    Last edited: Aug 12, 2019
  4. Oleksii_Serheiev

    Oleksii_Serheiev

    Joined:
    Mar 7, 2015
    Posts:
    9
    Hopefully fixed the issue!
    The root cause was that I have not tried to build and run with enabled Interpolation.
    Due to I have enabled Interpolation and tried it only with Unity emulator It produced jerking and I have thought Interpolation has not fixed the issue.

    So that, Guys, I would recommend trying to build with enabled Interpolation and run your game.