# Endless runner smooth movement with sphere

Hello everyone. I am trying to make an endless runner, where the player is a sphere.
The problem is that I can’t achieve a smooth movement at switching lanes and at jumping.
Sometimes it increases its speed when jumping, and when I switch between lanes it lags a little.
I tried with Update function, but it looks a little bit better with FixedUpdate.

``````void FixedUpdate () {
Vector3 position = rigidBody.position;

if (Input.GetKeyDown ("a") && moveHorizontal != -2.25f) {
moveHorizontal -= 2.25f;
}
if (Input.GetKeyDown ("d") && moveHorizontal != 2.25f) {
moveHorizontal += 2.25f;
}

position.x = Mathf.Lerp (position.x, moveHorizontal, 0.3f);
position.z = Mathf.Lerp (position.z, position.z + 0.5f, 0.25f);
rigidBody.position = position;

if (Input.GetKeyDown ("space")) {
if (currentlyJumping != true && jumps != 0){
currentlyJumping = true;
StartCoroutine (JumpActivator ());

jumps = jumps - 1;
FindObjectOfType<GameManager>().SetJumps (jumps);

Vector3 jump = new Vector3 (0f, 25f, -10f);
}
}
}
``````

My lanes are at -2.25, 0 and 2.25 on the X axis.

Thank you in advance for any type of help!

@danielsava
This could be a lot of things but here is a script in which keeps the speed from exceeding the set max speed:

``````    public int maxSpeed = 10;
public int movementSpeed = 75;
public float laneChangeDistance = 2.25f;
public float SpeedOfLaneChange = 10f;

private Rigidbody rb;
private Vector3 lastPosition;
private bool isMoving;
private bool allowedToAccelerate;
private float speed;
private Vector3 horizontal;
// Use this for initialization
void Start()
{
rb = GetComponent<Rigidbody>();
horizontal = Vector3.zero;
lastPosition = transform.position;
isMoving = false;
allowedToAccelerate = true;
}

// Update is called once per frame
void Update()
{
if (transform.position != lastPosition)
{
isMoving = true;
}

if (transform.position == lastPosition)
{
isMoving = false;
}

if (isMoving)
{
speed = rb.velocity.magnitude;
Debug.Log("Speed: " + speed);
if (speed >= maxSpeed)
{
//This keeps the speed from exceeding the maxspeed
rb.velocity = Vector3.ClampMagnitude(rb.velocity, maxSpeed);
allowedToAccelerate = false;
}

else
{
allowedToAccelerate = true;
}
}

if (Input.GetKeyDown(KeyCode.A))
{
horizontal = new Vector3(transform.position.x - laneChangeDistance, transform.position.y, transform.position.z);
//Vector3.Lerp(transform.position, horizontal, movementSpeed * Time.deltaTime);
transform.position = horizontal;
}

if (Input.GetKeyDown(KeyCode.D))
{
horizontal = new Vector3(transform.position.x + laneChangeDistance, transform.position.y, transform.position.z);
transform.position = horizontal;
//Vector3.Lerp(transform.position, horizontal, movementSpeed * Time.deltaTime);
}

if (allowedToAccelerate)
{
rb.AddForce(new Vector3(0f, 0f, 5f) * movementSpeed * Time.deltaTime, ForceMode.Acceleration);
}

if (Input.GetKey(KeyCode.Space) && allowedToAccelerate)
{
rb.AddForce(new Vector3(0f, 25f, -10f) * movementSpeed * Time.deltaTime, ForceMode.Acceleration);
}

lastPosition = transform.position;
}
``````

It could be the force mode when you add force to your rigid body. Try playing around with the different force modes and see what happens. Here is what each does:

Force: Add a continuous force to the rigidbody, using its mass.

Acceleration: Add a continuous acceleration to the rigidbody, ignoring its mass.

Impulse: Add an instant force impulse to the rigidbody, using its mass.

VelocityChange: Add an instant velocity change to the rigidbody, ignoring its mass.