Stutter on camera following character in top down game

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!

Have you tried using the Unity Profiler to see what happens during these bursts of stuttering? Are there any fluctuation there at all, or are you absolutely positive that it is a result of your scripting logic?

I tried using a different script (one that does not call FixedUpate) and the stutter is still there so maybe it’s not specific code that’s causing it.

I tried using the Profiler just now but nothing is really sticking out as far as I can tell. But I’m haven’t used it before so I’m not sure what I’m looking for. Here are some readings I got with all of the dynamic game stuff (dynamic lights, enemies) turned off except the player guy:
http://screencast.com/t/wZOQf7hYaWH
http://screencast.com/t/jRmiotqQ51aL

Is there anything unusual about these patterns ? I did notice that the stutter is less sever with the dynamic stuff turned off. But definitely still there. Any suggestions ?

Thanks!

What you are looking for are spikes at the time you notice the stuttering. It is natural for activity to go up and down, but frequent spikes or huge spikes that dip you into the red are no good and should be looked into. And as a point of reference it may not in fact be your Camera script that is causing it. Take for example your first screen shot, under CPU you have a quick spike that dropped you way below 60 FPS. It appears to be blue/turquoise in color.

When you see spikes, pause the game and you can click that section (click the CPU section) and move the time bar to the spike to see what was the cause of the majority of resource usage at that time. If going through your spikes you don’t see anything that matches up with the times you see the stuttering then we can start looking at your Camera script.