How to change the speed of "transform.position"?

Hello,

I have made a movement script for a 3lane runner type of game and I am stuck at how to make the transition between lanes slower. At the moment it is instant, it seems it is dependent by the runtime or frameupdate maybe because I am using “transform.position”. I have tried using “Time.scaletime” but it effect the speed of everything except the transition/laneSwitching. I also found this which is similar, but i did not get it working (link text).

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

public class Controllers : MonoBehaviour
{
	public float speed;
	public int laneCount = 0;
	public bool maxLaneLeft = false;
	public bool maxLaneRight = false;

	public Rigidbody rb;

	public void Start()
	{
	}

	public void Update()
	{
		rb.AddForce(new Vector3(0, 0, 1) * speed * Time.deltaTime);
		Movement();
	}

	public void Movement()
	{



		if (Input.GetKeyDown(KeyCode.A) && maxLaneLeft == false)
		{
			transform.position += new Vector3(-5, 0, 0);
			transform.eulerAngles = new Vector3(0, 0, 0);

			laneCount -= 1;

		}
		if (laneCount == -1 && maxLaneRight == false)
		{
			maxLaneLeft = true;
			maxLaneRight = false;
		}
		if (Input.GetKeyDown(KeyCode.D) && maxLaneRight == false)
		{
			transform.position += new Vector3(5, 0, 0);
			transform.eulerAngles = new Vector3(0, 0, 0);
		

			laneCount += 1;
		}
		if (laneCount == 1 && maxLaneLeft == false)
		{
			maxLaneLeft = false;
			maxLaneRight = true;
		}
		if (laneCount == 0)
		{
			maxLaneLeft = false;
			maxLaneRight = false;
		}
	}
}

Thanks in advance <3

You are directly setting transform.position when the user presses a key. This is literally moving the object from one position to another. That is why you cannot “slow” it down. If you wanted a smooth transition while still only requiring one key press, you would need to either start a coroutine that changes transform.position by a very small amount each frame, or call a method in Update that moves your transform by a small amount each Update when a condition is true. The link you provided has a great example of how you could write a coroutine to do something like this in the comment of the accepted answer.

enjoy. alt text

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

public class LaneMover : MonoBehaviour {
     public float startspeed=10;
     public int lane = 0;
     public int maxLaneLeft=-1,maxLaneRight = 1;
	 public float stepsize = 5;
	 float desiredxpos=0;
 
     public Rigidbody rb;
 
     public void Start()
     {
		 rb.AddForce(new Vector3(0, 0, 1) * startspeed);
     }
 
     public void Update()
     {
         
		 In();
         Movement();
     }
 
	void In(){
         if (Input.GetKeyDown(KeyCode.A) && desiredxpos > maxLaneLeft*stepsize)
         {
			 desiredxpos -= stepsize;
             lane -= 1;
         }
         if (Input.GetKeyDown(KeyCode.D) && desiredxpos < maxLaneRight*stepsize)
         {
			 desiredxpos += stepsize;
             lane += 1;
         }
     }
	
     public void Movement()
     {
		 transform.position = new Vector3(Mathf.Lerp(transform.position.x,desiredxpos,0.1f),transform.position.y,transform.position.z);
	 }
}