2D Smooth Camera Follow

Hello all,

I’m very new to coding and unity in general but i’ve started my first game and have run into a bit of a problem. It’s a 2D platformer, i have my player (cube), 2 platforms (one the ground, another smaller in the air to jump onto, both cubes as well), the script to move/jump my player, and camera script to follow my player.

The problem is the “lag” i get from the camera i am guessing. At first my player and the background (platforms) lagged as i ran or jumped. I have upped the damping and now the player is very smooth but the platforms still lag.

Any suggestions how to fix it or a better way to attach the camera to the player for a 2D platformer would be very much appreciated, thanks for your time.

Using C# btw,
Here is my Camera Script:

using UnityEngine;

using System.Collections;



        public class SmoothFollow2 : MonoBehaviour 

{

        public Transform target;

        public float distance = 3.0f;

        public float height = 3.0f;

        public float damping = 5.0f;

        public bool followBehind = true;



        void Update () 

		{

               Vector3 wantedPosition;

               if(followBehind)

                       wantedPosition = target.TransformPoint(0, height, -distance);

               else

                       wantedPosition = target.TransformPoint(0, height, distance);

     

               transform.position = Vector3.Lerp (transform.position, wantedPosition, Time.deltaTime * damping);

        }

}

Change your method from Update to LateUpdate.

Thanks for the quick reply, i’ve tried the LateUpdate as well before but it doesn’t seem to fix it.

I have found that my Player script was under FixedUpdate for some reason and switched it to Update, also switched the Camera back to LateUpdate like you said and it has definatly helped a bit but the platforms still seem to lag, just not as bad.

Any other suggestions?

Alright nvm, it lags when run in Unity but when i build the exe and run it, it’s fine. Thanks Big, appreciate the help.

After tinkering around with it, i got the same results. So, I just fixed the camera to the object. (parented it)

I really think it is the Lerp causing it to shake a bit.