# How to make camera smooth when following player

When my character moves through my cameras 'FollowPlayer' script, the background just looks really glitchy and bad Is there anyway to fix this?

This is my script:

using UnityEngine;
using System.Collections;
public class FollowPlayer : MonoBehaviour {
Transform bar;

void Start() {
bar = GameObject.Find("PlayerMovingBar").transform;
}

void Update() {
transform.position = new Vector3(
bar.position.x, transform.position.y, transform.position.z);
}
}

• Can you describe the symptoms of "really glitchy and bad" in more detail? It's not obvious from your code what might be going wrong. Including an animated gif or a link to a short video of the problem can often help remove ambiguity when describing visual/animation artifacts. – DMGregory Aug 23 '17 at 16:29
• Here is a really useful program for taking short screen recordings, and saving them as gifs. Take a recording of the background, and post it so we can see what you mean by "glitchy". As @DMGregory points out, you also want to describe "glitchy", to make your question understandable. – Gnemlock Aug 23 '17 at 23:52
• You also want to have a look into "Smooth Follow"; you do not seem to be applying any smoothing, and thus, it is going to look odd. The default Unity packages contain an example script. – Gnemlock Aug 23 '17 at 23:53
• well basically if im stationary the backround looks fine but right when I move it looks like the backround is moving up and down rapidly. I just want it smoother so it doesnt move up and down – shrookypranks Aug 25 '17 at 0:49

Using Lerp will give a smooth following effect as @Hellium mentioned. It's also recommended to use it in the LateUpdate() method.

See Unity documentation for that : MonoBehaviour.LateUpdate()

You may simply need to Lerp to the desired position :

public float followSpeed = 10 ; // Change the value here to have the desired result

void Update() {
Vector3 targetPosition = new Vector3(bar.position.x, transform.position.y, transform.position.z);
transform.position = Vector3.Lerp( transform.position, targetPosition, followSpeed * Time.deltaTime );
}

• when I add that to my script the pubic followspeed turns red and the follow speed in the other part of it – shrookypranks Aug 23 '17 at 17:35
• Yes, indeed, sorry, I have forgotten the type of the variable.... – Hellium Aug 23 '17 at 18:41
• well I added that to the script and it still seems like the backround is jumping around and moving when I move – shrookypranks Aug 23 '17 at 18:53
• The problem may not come from the script then. Please, provide additional information about your problem (gif or video) and the setup of your scene. – Hellium Aug 23 '17 at 19:31
• The deltaTime correction you've applied is not correct for the exponential ease-in style of Lerp you're using, which can cause the scene to judder on a variable framerate. You can use the formula for weight in this answer to correct it. – DMGregory Aug 23 '17 at 19:33