Camera controller small problem

I’m working on a 2d game and having a problem scripting the camera controller.

basically what i’m trying to do is to make an if statement to check that if the camera x position isn’t as same as the main player x pos’, move the camera on the X axis towards the player.

problem is that the camera goes all blurry, because the camera move too much.

    public void Camera_Controller()
    {
        float distance = Mathf.Abs(Vector2.Distance(player.position, transform.position));

        if (distance > 0)
        {
            if(player.position.x < transform.position.x)
            {
                transform.position -= new Vector3(10*Time.deltaTime,0);
            }
            else
            {
                transform.position += new Vector3(10*Time.deltaTime, 0);
            }
        }
    }

Use something like SmoothDamp or MoveTowards to get nice, smooth camera movement.

2 Likes

Thanks for your help.
MoveTowards works like charm! so smooth and nice. what’s the difference between MoveTowards and SmoothDamp?

new code if it may be helpful for someone:

    void Update()
    {
        Camera_Controller();
    }

    public void Camera_Controller()
    {
        distance = Mathf.Abs(Vector2.Distance(player.position, transform.position));
        Vector3 targetPos = new Vector3(player.position.x, transform.position.y, -10f);

        if(distance > 0)
        {
            transform.position = Vector3.MoveTowards(transform.position, targetPos , 2*Time.deltaTime);
        }
    }

SmoothDamp has “weight” to it, it’s not a linear A to B movement.