Move an object using GetKeyDown gradually

So I am creating a 2d game where basically the player is driving a car and must change lanes to avoid collisions with other cars. I’m using the arrow keys that when you press the left arrow key, you move one lane left, and when you press the right arrow key, you move one lane right. The code I’m currently using is

function Update () {
	if (Input.GetKeyDown (KeyCode.LeftArrow)) transform.Translate (Vector2(-1.2,0));
    if (Input.GetKeyDown (KeyCode.RightArrow)) transform.Translate (Vector2(1.2,0));
	}

this works except that vehicles don’t teleport when they change lanes, they gradually move to the next lane. How would I go about having an object move gradually when a key is pressed?

So it looks like you want the object to move over 1.2 units in either direction when you press the arrows.

Try storing the position that you want to move to (1.2 units away from the cars current position) into a variable, and then each frame, translate the car by a fixed amount until reaching that goal position, and then stop moving!

Do you need example code for this or does that make sense?

try creating a function and calling the if have pressed the button, seting a boolean true
like this :

#pragma strict
var translating : boolean;
var amount : float;
var time : float;
var timetotranslate : float = 0.3;
function Update () {

    if (Input.GetKeyDown (KeyCode.LeftArrow)&&!translating) 
    {
    amount = -1.2;
    translating = true;
    }
    if (Input.GetKeyDown (KeyCode.RightArrow)&&!translating) 
    {
    amount = 1.2;
    translating = true;
    }
    
    if(translating)
    {
    time += Time.deltaTime;
    smoothTranslate(Vector2(amount,0),timetotranslate);
    
    }
    
    }
    
function smoothTranslate(amount : Vector2,delay : float)
{
if(time<=delay)
transform.Translate (amount*Time.deltaTime/delay);
else
{
time = 0;
translating = false;
}
}

Vector2.MoveTowards(); should work for you.

IEnumerator moveTo(float posX,float posY, float speed)
{
	while((transform.position.x != posX) && (transform.position.y != posY))
		{
			transform.position = Vector2.MoveTowards (transform.position,new Vector2(posX,posY),speed * Time.deltaTime);
			yield return null;
		}	
}

And call it with:

StartCoroutine( moveTo(targetX,targetY,speed) );

So in your case here you’d go:

if (Input.GetKeyDown (KeyCode.LeftArrow))
{
StartCoroutine( moveTo(transform.position.x + 1.2,0,20) );
}
else if (Input.GetKeyDown (KeyCode.LeftArrow))
{
StartCoroutine( moveTo(transform.position.x - 1.2,0,20) );
}

I believe I have found my solution. I stumbled upon the plugin iTween and it seems to be helping a lot.