Cannot Achieve Smooth Infinite Movement
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.
Answer by Salmjak · Feb 14, 2016 at 10:16 PM
You shouldnt really move your camera with a rigidbody...
Update your _playerRB.velocity in FixedUpdate(). Then set your Camera.main.transform.position = _playerRB.position. Use an intermediate variable (Vector3 newPos) so you can set an offset/distance for the camera. Ex:
Vector3 newPos = _playerRB.position;
newPos.z -= 10f; //Think it's negative is infront in 2D-view.
Camera.main.transform.position = newPos;
You could also smooth camera movement with Vector3.lerp(). In that case it would look like this:
float Lerprate = 15f;
Vector3 newPos = _playerRB.position;
newPos.z -= 10f; //Think it's negative is infront in 2D-view.
Camera.main.transform.position = Vector3.lerp(Camera.main.transform.position, newPos, Time.deltaTime*lerpRate);
Yes, I know it is not common practice to add physics components to the camera but the point I was trying to make was that I have basically exhausted every way I could find of moving transforms in Unity and still encountering the same problem. As I said in my post, I have already tried updating the position of the camera transform according to the player and the jerkiness of the motion is still there. Surprisingly, it actually produces even more jittery results in my project and I cannot figure out why. I also did try the lerp approach you mentioned and it made no difference.
Was just making sure. You don't have the camera as a child, do you?
No sir. I didn't set it as a child because it's only supposed to follow the player in the x direction, not y. I did try that though just to see if it still looked bad and it does. I'm completely baffled at this point.
@dan5071 enabling kinematic turns off physics calculations. Since youre moving at a high speed (relatively) it might be that the physics engine cant keep up and stutters.