Hi guys,
I’m working on a 2d topdown game where the camera follows the player. The behavior is defined in a script rather than the camera being a child of the player object. I have this problem where my camera will follow the player smoothly for most of the time. Then all of a sudden you get these short “bursts” of stuttering that are unrelated to the frame rate. They also don’t affect the player character (rigidbody2d set to interpolate by the way) - just everything else in the scene. It’s very annoying. I’m already using some code I found in some other thread to make my camera smoother. Still can anybody share their ready-made code that I can also try out - this is really getting on my nerves. Here’s my code:
public class CameraMovement : MonoBehaviour
{
Transform target;
private float calculateCamPositionTime;
private float speed = 1f;
private Vector3 camPlaceHolder;
void Awake ()
{
target = GameObject.Find (Tags.PLAYER).transform;
transform.position = target.position;
}
void Start ()
{
camPlaceHolder = gameObject.transform.position;
}
void FixedUpdate ()
{
calculateCamPositionTime = Time.time; //Time in seconds since game started
float interpAmount = speed * Time.deltaTime; //Time in seconds it took to complete the last frame
camPlaceHolder = Vector3.Lerp (camPlaceHolder, new Vector3 (target.position.x, target.position.y, -10f), interpAmount);
}
void LateUpdate ()
{
float timeSinceLastFixedUpdate = Time.time - calculateCamPositionTime;
transform.position = camPlaceHolder + (Camera.main.velocity * timeSinceLastFixedUpdate);
}
}
Thanks for your help!