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:
Player’s RigidBody2D:
public class PlayerController : MonoBehaviour
{
[SerializeField]
private float speed = 1;
[SerializeField]
private Vector2 lookDirection = new Vector2(1, 0);
private Rigidbody2D rigidbody2d;
private Animator animator;
// Start is called before the first frame update
void Start()
{
rigidbody2d = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
}
void FixedUpdate()
{
float horizontalMovement = Input.GetAxis("Horizontal");
Vector2 movement = new Vector2(horizontalMovement, 0.0f);
Vector2 position = rigidbody2d.position + movement * speed * Time.fixedDeltaTime;
if (!Mathf.Approximately(horizontalMovement, 0.0f))
{
lookDirection.x = horizontalMovement;
lookDirection.Normalize();
}
animator.SetFloat("Speed", movement.magnitude);
animator.SetFloat("Direction", lookDirection.x);
rigidbody2d.MovePosition(position);
}
}
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.