Weird problem with camera follow script

So I am trying to
make a smooth follow camera

public class CameraFollow : MonoBehaviour
{
Transform player;
Transform cam;
float x = 4f;
float y = 5f;
float smoothX;
float smoothY;
float velocity = 0;
public float smoothTime = 0.05f;

void Awake()
{
cam = GetComponent<Transform>();
player = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
}

void FixedUpdate()
{
smoothX = Mathf.SmoothDamp(cam.position.x, player.position.x, ref velocity, smoothTime, 15f);
smoothY = Mathf.SmoothDamp(cam.position.y, player.position.y, ref velocity, smoothTime, 15f);
}

void LateUpdate()
{
cam.position = new Vector3(Mathf.Clamp(smoothX, -x, x), Mathf.Clamp(smoothY, -y, y), -10f);
}
}

But its behaving weird when I move right or up. For example if I move to the right it seems to follow with some weird offset on the y (if that makes sense :P)
However it works perfect when moving left or down.

Here’s a little demo of it:
https://googledrive.com/host/0Bzct6cQnrUORbmNwTWFSU

You should really have separate ‘velocity’ floats for x and y - using the same one for both might cause problems with the SmoothDamp functionality if it uses that internally.

1 Like

That did it! Thank you very much :slight_smile: