Need Help with coding: When i play my games character the character doesn’t stay stable while changing sides

When i play my games character the character doesn’t stay stable while changing sides.

Can someone tell me what am i doing wrong with the code?

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

public class PlayerController : MonoBehaviour
{
    private CharacterController controller;
    private Vector3 direction;
    public float forwardSpeed;

    private int desiredLane = 1; // 0:left 1:middle 2:right
    public float laneDistance = 4; // the distance between two lanes


    public float jumpForce;
    public float Gravity = -20;

    void Start()
    {
        controller = GetComponent<CharacterController>();
    }

    // Update is called once per frame
    void Update()
    {
        direction.z = forwardSpeed;

        direction.y += Gravity * Time.deltaTime;
        if (Input.GetKeyDown(KeyCode.UpArrow) && Physics.Raycast(transform.position, -Vector3.up, 0.1f))
        {
            Jump();
        }

        // Gather the inputs on which lane we should be
        

        if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            desiredLane++;
            if (desiredLane == 3)
                desiredLane = 2;
        }

        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            desiredLane--;
            if (desiredLane == -1)
                desiredLane = 0;
        }

        // Calculate where we should be in the future
        Vector3 targetPosition = transform.position.z * transform.forward + transform.position.y * transform.up;

        if (desiredLane == 0)
        {
            targetPosition += Vector3.left * laneDistance;
        }
        else if (desiredLane == 2)
        {
            targetPosition += Vector3.right * laneDistance;
        }

        transform.position = Vector3.Lerp(transform.position, targetPosition, 60 * Time.deltaTime);
    }

    private void FixedUpdate()
    {
        controller.Move(direction * Time.fixedDeltaTime);
    }

    private void Jump()
    {
        direction.y = jumpForce;
    }
}

The issue you’re experiencing with your character not staying stable while changing sides might be due to how you’re calculating the target position for the character’s movement between lanes.

In your code, you’re calculating the targetPosition based on the current position of the character using transform.position.z * transform.forward + transform.position.y * transform.up. However, this calculation doesn’t take into account the current X position of the character, which might result in unstable movement when changing lanes.

To fix this issue, you should calculate the targetPosition based on the current position of the character in all three dimensions (X, Y, Z). Here’s how you can modify your code to achieve this:

// Update is called once per frame
void Update()
{
    direction.z = forwardSpeed;

    direction.y += Gravity * Time.deltaTime;
    if (Input.GetKeyDown(KeyCode.UpArrow) && Physics.Raycast(transform.position, -Vector3.up, 0.1f))
    {
        Jump();
    }

    // Gather the inputs on which lane we should be
    

    if (Input.GetKeyDown(KeyCode.RightArrow))
    {
        desiredLane++;
        if (desiredLane == 3)
            desiredLane = 2;
    }

    if (Input.GetKeyDown(KeyCode.LeftArrow))
    {
        desiredLane--;
        if (desiredLane == -1)
            desiredLane = 0;
    }

    // Calculate where we should be in the future
    Vector3 targetPosition = transform.position;

    if (desiredLane == 0)
    {
        targetPosition += Vector3.left * laneDistance;
    }
    else if (desiredLane == 2)
    {
        targetPosition += Vector3.right * laneDistance;
    }

    transform.position = Vector3.Lerp(transform.position, targetPosition, 60 * Time.deltaTime);
}

In this modification, we’re calculating the targetPosition based on the current position of the character (transform.position) and then adjusting the X component of the targetPosition based on the desired lane. This ensures that the character stays stable while changing sides.

How about

private Vector3 m_velocity;
const float smoothtime = 0.2f;

targetPosition = Vector3.left * (ladeDistance * desiredLane);
transform.position = Mathf.SmoothDamp(transform.position, targetPosition, ref m_velocity, smoothTime);

Btw this whole “lerp with Time.deltaTime as factor” thing is a very commonly shared misconception that will never lead to stable results. Don’t.