My Player is Flickering with use of Vector3 .

Hello i am new to unity and still fighting with basics…
So in m game i see my game object Flickers as it moves in straight line.
I have tried Fixed Update or *Time.deltaTime with no results.

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

public class BallController : MonoBehaviour
{


    public static bool LevelSelected;
    public static float speed;
    [SerializeField]
    bool started;
    bool gameOver;


    
    Rigidbody rb;

    void Awake()
    {  
        rb = GetComponent<Rigidbody>();
    }

    // Use this for initialization
    void Start()
    {
        started = false;
        LevelSelected = false;
        gameOver = false;
    }

    // Update is called once per frame
     void Update()
    {

        if (LevelSelected)
        {
            if (!started)
            {

                if (Input.GetMouseButtonDown(0))
                {
                    rb.velocity = new Vector3(speed, 0, 0);
                    started = true;

                    GameManager.instance.StartGame();

                }
            }
        }


        if (Input.GetMouseButtonDown(0) && !gameOver)
        {
            SwitchDirection();
        }
    }

    void SwitchDirection()
     {
         if (rb.velocity.z > 0)
         {
            rb.transform.Rotate(0.0f, 90.0f, 0.0f, Space.Self);
            rb.velocity = new Vector3(speed, 0, 0);
         }
         else if (rb.velocity.x > 0)
         {
            rb.transform.Rotate(0.0f, -90.0f, 0.0f, Space.Self);
            rb.velocity = new Vector3(0, 0, speed);
         }
     }

    public void StopMoving()
    {
        rb.useGravity = true;
        rb.isKinematic = false;


        Camera.main.GetComponent<BallFollow>().gameOver = true;

    }
}

And this is my Camera Follow Code

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

public class BallFollow : MonoBehaviour
{

    public GameObject ball;
    Vector3 offset;
    public float lerpRate;
    public bool gameOver;

    // Start is called before the first frame update
    void Start()
    {
        offset = ball.transform.position - transform.position;
        gameOver = false;
    }

    // Update is called once per frame
    void Update()
    {
        if (!gameOver)
        {
            Follow();
        }
    }

    void Follow()
    {
        Vector3 pos = transform.position;
        Vector3 targetPos = ball.transform.position - offset;
        pos = Vector3.Lerp(pos, targetPos, lerpRate * Time.deltaTime);
        transform.position = pos;
    }
}

Your problem is here :

void SwitchDirection()
          {
              if (rb.velocity.z > 0)
              {
                 rb.transform.Rotate(0.0f, 90.0f, 0.0f, Space.Self);
                 rb.velocity = new Vector3(speed, 0, 0);
              }
              else if (rb.velocity.x > 0)
              {
                 rb.transform.Rotate(0.0f, -90.0f, 0.0f, Space.Self);
                 rb.velocity = new Vector3(0, 0, speed);
              }
          }

While the button is pressed, it triggers alternatively the top part and the bottom part at each update.

void Update()
{
if (Input.GetMouseButtonDown(0))
{
// Move your code to a method so you can reuse elsewhere
// Use transform.forward to make it move towards your gameobject Z axis
Move(transform.forward);
}

        if (Input.GetMouseButtonDown(0))
            SwitchDirection();
    }
    // New method to handle movement
    private void Move(Vector3 direction)
    {
        // By multiplying a Vector3 by speed you can move your gameobject towards that Vector3
        rb.velocity = direction * speed;
    }
    void SwitchDirection()
    {
        if (rb.velocity.z > 0)
        {
            rb.transform.Rotate(0.0f, 90.0f, 0.0f, Space.Self);
            // remove this line, you just need to rotate it, movement is already being handled.
            //rb.velocity = new Vector3(speed, 0, 0); 
        }
        else if (rb.velocity.x > 0)
        {
            rb.transform.Rotate(0.0f, -90.0f, 0.0f, Space.Self);
            // remove this line, you just need to rotate it, movement is already being handled.
            //rb.velocity = new Vector3(0, 0, speed);
        }
    }

Also make sure to make to freeze your rigidbody rotation in the X and Z axis like this:
159002-unity-rigidbody-constraints.png


Or alternatively make a Physics Material and drop it in your colliders material slot, set its dynamic friction to something like 0.2 to stop your rigidbody from having too much friction with the ground.
159003-unity-physics-material-3d.jpg