endless runner movement problem

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

public class PlayerMotor : MonoBehaviour {

    private CharacterController controller;
    private Vector3 moveVector;

    public float smooth = 2;
    

    private float speed = 5.0f;

    private float verticalVelocity = 0.0f;
    private float gravity = 12.0f;
	private float jumpForce = 5.0f;

	// Use this for initialization
	void Start () {
        controller = GetComponent<CharacterController>();
        
	}
	
	// Update is called once per frame
	void Update () {
        moveVector = Vector3.zero;

        if (controller.isGrounded)
        {
            verticalVelocity = -0.5f;
			verticalVelocity = -gravity * Time.deltaTime;
			if (Input.GetKeyDown (KeyCode.Space)) {
				verticalVelocity = jumpForce;
			}
        }
        else
        {
            verticalVelocity -= gravity* Time.deltaTime;
        }
        //X
        if (Input.GetKeyDown(KeyCode.A))
        {
            
            Vector3 newPosition = controller.transform.position;
            newPosition.x--;
            controller.transform.position = Vector3.Lerp(transform.position, newPosition, smooth);
        }

        if (Input.GetKeyDown(KeyCode.D))
        {
            
            Vector3 newPosition = controller.transform.position;
            newPosition.x++;
            controller.transform.position = Vector3.Lerp(transform.position, newPosition, smooth);
        }
        //Y
        moveVector.y = verticalVelocity;
        //Z
        moveVector.z = speed;


        controller.Move(moveVector * Time.deltaTime);
		
	}
}

I found a problem, the character moves to shoot even though I use the lepr code.
How can I fix it?

The problem is that when I press A or D doesn’t a fluid motion

Ok somthing like this then ?

new Vector3 NewPos;
	// Update is called once per frame
	void Update () 
	{
		if (Input.GetKeyDown(KeyCode.D))
        {
			  NewPos = transform.position;
              NewPos.x += 5;    
		}

		transform.position = Vector3.Lerp(transform.position, NewPos, 2f * Time.deltaTime);
	}
}

See what you are doing is trying to move withing 1 frame when you press A||D. This way its doing it over many frames so its smooth but this way has an issue of rounding. You see LERP will goes towards target but never gets there 0->5 (Look in the inspector and you will ses what i mean). Now you can Code in Snap function so when object gets close to target position you can force it so from 0.49 to 5. This is how i fix it. Hopefully this helped you @spigo98 a little.

Hello, sorry for a bit late response, but it’s now that I’m working on a endless runner game for a school project.

I’m also posting this for anyone else who is checking the forums that might be having issues with this case and could benefit from my input, since I’ve looked and found an answer to my problems countless times and I just want to return to community.

I’ve written this code for moving a character quite smoothish(probably could be even smoother) from point A to B and B to C and vice versa . This code also responds if a player wants to change direction of character when its already moving towards a certain lane. For example he wants to go from lane A to B but while character is moving to B he wants to go back to A. I hope this will help u in anyway.

//This is all in Update Function
// I have a 3 lane runner { -1, 0, 1 }, checking so it doesn't gets over that
    if (Input.GetKeyDown(moveL)  && wantedRow > -1)
            {
                centeringZero = false;
                wantedRow--;
            }
            if (Input.GetKeyDown(moveR)  && wantedRow < 1)
            {
                centeringZero = false;
                wantedRow++;
            }
            if (wantedRow == -1)
                wantedX = -horiDiff;
            else if (wantedRow == 0)
                wantedX = 0;
            else if (wantedRow == 1)
                wantedX = horiDiff;
    //bool centeringZero is necessary to prevent update of doing a multiple movement changes at once, since update is a bit unpredictable and same thing can be set multiple times. So I find bools quite useful in Update functions.
            if (centeringZero)
            {
                transform.Translate(new Vector3(0, 0, moveSpeed) * Time.deltaTime);
                Debug.Log("0, 0, moveSpeed");
            }
            else if (transform.localPosition.x < wantedX && !centeringZero)
            {
                transform.Translate(new Vector3(3, 0, moveSpeed) * Time.deltaTime);
                Debug.Log("translate 3,0,movespeed");
                if (wantedRow == 0)
                {
    //if statement to check if its close to zero if it is its sets it to zero.
                    if (transform.localPosition.x >= -0.1f && transform.localPosition.x <= 0.1f && !centeringZero)
                    {
                        Vector3 fixX = transform.localPosition;
                        fixX.x = 0;
                        transform.localPosition = fixX;
                        currRow = 0;
                        centeringZero = true;
                        Debug.Log("centering 0");
                    }
                }
                if(wantedRow == 1)
                {
    //if statement to check if its close to horiDiff in my case its set to 1.25f and set it to 1.25f
                    if (transform.localPosition.x < horiDiff && transform.localPosition.x > horiDiff - 0.1f && !centeringZero)
                    {
                        Vector3 fixX = transform.localPosition;
                        fixX.x = horiDiff;
                        transform.localPosition = fixX;
                        currRow = 1;
                        centeringZero = true;
                        Debug.Log("centering " + horiDiff);
                    }
                }
                
            }
            else if(transform.localPosition.x > wantedX && !centeringZero)
            {
                transform.Translate(new Vector3(-3, 0, moveSpeed) * Time.deltaTime);
                Debug.Log("translate -3,0,movespeed");
                if (wantedRow == 0)
                {
    //if statement to check if its close to 0 and sets it to 0
                    if (transform.localPosition.x >= -0.1f && transform.localPosition.x <= 0.1f && !centeringZero)
                    {
                        Vector3 fixX = transform.localPosition;
                        fixX.x = 0;
                        transform.localPosition = fixX;
                        currRow = 0;
                        centeringZero = true;
                        Debug.Log("centering " + 0);
                    }
                }
                if (wantedRow == -1)
                {
    //if statement to check if its close to horiDiff in my case its set to 1.25f and set it to 1.25f
                    if (transform.localPosition.x > -horiDiff && transform.localPosition.x < -horiDiff + 0.1f && !centeringZero)
                    {
                        Vector3 fixX = transform.localPosition;
                        fixX.x = -horiDiff;
                        transform.localPosition = fixX;
                        currRow = -1;
                        centeringZero = true;
                        Debug.Log("centering " + -horiDiff);
                    }
                }
            }

If you want smoother transitions from one lane to another, only thing that could be causing uneven transition is when function fixes character position to its lane. You can play around with if statements where it checks if its close to desired character X position and then set its position.

//try changing this to even closer to horiDiff, perhaps 0.01f - could be better but not sure
if (transform.localPosition.x < horiDiff && transform.localPosition.x > horiDiff - 0.1f && !centeringZero)
                     {
                         Vector3 fixX = transform.localPosition;
                         fixX.x = horiDiff;
                         transform.localPosition = fixX;
                         currRow = 1;
                         centeringZero = true;
                         Debug.Log("centering " + horiDiff);
                     }

I’ve tried to explain the code and logic best as I can, but if there is still anything unclear, please tell me where and I’ll try to explain it as best as I can.
Again this could be probably done smoother/better and definitely cleaner, but I’m kinda rushing to finish it as fast as I can, since project due is closing in fast, so this will work for me.