I am making a platformer game and after about a week of trying several hours per day, I cannot seem to make the player and camera move smoothly to to right. I thought at first it could just be too many scripts running at the same time in my project, so I made a new dirt-simple project to see if the problem was replicated. The new project consists of the main camera, a sphere as the player, and a few immobile cubes to see the movement better. Nothing else. Here is a screenshot to demonstrate the simplicity:
Without fail, every 2-3 seconds there is a sharp and very noticeable stutter in the movement. Right now, I have the movement set up by updating the velocity of the rigidbodies attached to the player and the camera. However, I have tried every approach I could think of by using Translate(), updating transform.position, using AddForce() on the rigidbodies, etc. and the problem still persists. Also, I have tried using Update(), LateUpdate(), and FixedUpdate() and the problem exists in all of them. Here is my current code (attached to the player), which I cannot imagine could be optimized any more.
using UnityEngine;
using System.Collections;
public class TestMotion : MonoBehaviour
{
private Rigidbody2D _playerRB;
private Rigidbody2D _cameraRB;
private Vector2 _velocity;
void Start ()
{
_playerRB = gameObject.GetComponent<Rigidbody2D>();
_cameraRB = Camera.main.GetComponent<Rigidbody2D>();
_velocity = new Vector2( 200.0f * Time.deltaTime, 0.0f );
}
void LateUpdate()
{
_playerRB.velocity = _velocity;
_cameraRB.velocity = _velocity;
}
}
I read that this problem occurs for some people in the editor, but not when their game is published, so I tried porting the project to Android and iOS but the problem is there too. It is unfathomable to me that such a seemingly simple problem has been so difficult to solve. I am consistently running at between 68-70 FPS in the game view, so I don’t think the issue is related to the frame rate. Also, my computer has a 4.5 GHz CPU, a GPU with 12GB VRAM, and 32GB RAM, so even if the issue was related to unoptimized code I wouldn’t expect it to be so noticeable. I am desperate for a solution because I really want to publish my game. Any help in figuring out what my problem is would be greatly appreciated.