Move Camera How?

All I want is to create a 2d game in which a button is pressed, the camera will move at a certain distance and then stop. For example when I press right, the camera will move towards the right and will stop after moving 10f.

this is what my code looks like. Is there a better solution since this doesn’t work 100% of the time.

public GameObject cam;    
public float speed;

void Update()
{
    if(moveR == true)
    {                                             
        cam.transform.Translate(speed * Time.deltaTime, 0, 0);    
        StartCoroutine(ghost());       
    }

    if(moveL == true)
    {    
        cam.transform.Translate(-speed * Time.deltaTime, 0, 0); 
        StartCoroutine(ghost());                                    
    }

    if(moveU == true)
    {
        cam.transform.Translate(0, speed * Time.deltaTime, 0); 
        StartCoroutine(ghost());                                
    }       
    
    if(moveD == true)
    {
        cam.transform.Translate(0, -speed * Time.deltaTime, 0); 
        StartCoroutine(ghost());                                    
    }                 
}

IEnumerator ghost()
{
    yield return new WaitForSeconds(0.2f);        
    moveR = false;
    moveL = false;
    moveU = false;
    moveD = false;  
}


public void moveRight()
{
    moveR = true;     
}

public void moveLeft()
{
    moveL = true;
}

public void moveUp()
{
    moveU = true;
}
  
public void moveDown()
{
    moveD = true;
}

I’d have a target variable and move the camera toward it every frame. Here’s an idea:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraMoveExample : MonoBehaviour
{
    public GameObject cam;
    public float distance = 10f;
    public float speed = 20f;

    private Vector2 target;

    private void Start()
    {
        target = cam.transform.position;
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            MoveRight();
        }
        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            MoveLeft();
        }
        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            MoveUp();
        }
        if (Input.GetKeyDown(KeyCode.DownArrow))
        {
             MoveDown();
        }

        transform.position = Vector2.MoveTowards(transform.position, target, speed * Time.deltaTime);
    }

    private void MoveRight()
    {
        target += Vector2.right * distance;
    }

    private void MoveLeft()
    {
        target += Vector2.left * distance;
    }

    private void MoveUp()
    {
        target += Vector2.up * distance;
    }

    private void MoveDown()
    {
        target += Vector2.down * distance;
    }
}

Hope this helps!