Camera follow issue.

So, I have a camera that has to follow the player that is a rolling ball, and i have the following script:

    public GameObject target;
    public Transform targetTransform;
    public Rigidbody targetRigidbody;
    
    private Transform camTransform;

    private float distance = 20.0f;
    private float height = 5.0f;
    private float rotationDamping = 0.2f;
    private float heightDamping = 10;

    void Awake()
    {
        camTransform = this.gameObject.GetComponent<Transform>();

        target = GameObject.FindWithTag("Player");
        targetTransform = target.GetComponent<Transform>();
        targetRigidbody = target.GetComponent<Rigidbody>();
    }

    void update(){
    //set the starting values

    float targetRotation;
    float targetHeight = targetTransform.position.y + height;
    float camRotation = camTransform.eulerAngles.y;
    float camHeight = camTransform.position.y;

    Vector3 flatSpeed= targetRigidbody.velocity;
    flatSpeed.y = 0;                         //flat speed of the player

    if (move == Vector3.zero)
        targetRotation = camRotation;
    else                                    //record Y rotation of flat speed if it is moving           
        targetRotation = Quaternion.LookRotation(move).eulerAngles.y;

    camHeight = Mathf.Lerp(camHeight, targetHeight, heightDamping * Time.deltaTime);
    camRotation = Mathf.LerpAngle(camRotation, targetRotation, rotationDamping * Time.deltaTime);

    Quaternion currentRotation;    //get the wanted rotation obtained through lerping
    currentRotation = Quaternion.Euler(0, camRotation, 0);

    Vector3 wantedPos = targetTransform.position - (currentRotation * Vector3.forward * distance);
    wantedPos.y = camHeight;
    camTransform.position = wantedPos;
    camTransform.LookAt(targetTransform);
                              //set position and rotation of the camera transform
}

now, this scripts works preatty fine, it follows smoothly the ball from the back while it moves.
my only problem is that, when i make the ball roll backward instead of forward the camera rotates 180 degrees and follows the player from the front insted, as you would expect. this gets the game quite confusing, and I really don’t know how to fix this script. Or if anyone knows of a better script and could be so kind to post it here.
Thank you in advance.

No exactly sure how you want the camera to follow the ball, but here is some very simple code:

public Transform target;

	void Update()
	{
		if (target)
		{
			transform.position = Vector3.Lerp (transform.position, target.position, 0.1f)+ new Vector3 (0, 0, -10);
		}
	}
}

Don’t forget to set your ball as the target in the inspector!

Ok I finally found out a solution, but I warn you thins only works on a rolling ball behaving like a wheel, like mine (it moves spinning only on its x axis); It’s quite convoluted so i’ll try to explain: since my ball spins on the on the x axis, I thought that rotating it by 90 degrees would turn it roughly into the “front” of the ball. so i just set as that the rotation of the camera around the ball.

Here’s the code:

private Transform playerTransform;
private Transform camTransform;

private float distance = 20.0f;
private float height = 5.0f;
private float rotationDamping = 0.2f;
private float heightDamping = 10;

void Awake()
{
  camtransform = gameObject.GetComponent<Transform>();
  playerTransform = gameObject.FindWithTag("Player").GetComponent<Transform>();
}

void Update()
{
  float currentAngle = camTransform.eulerAngles.y;
  float wantedHeight = targetTransform.position.y + height;

  Vector3 frontDirection = Quaternion.Euler(0, -90, 0) * targetTransform.right;
  frontDirection.y = 0; //flat vector just to be sure

  float wantedAngle = Quaternion.LookRotation(frontDirection).eulerAngles.y;

  //interpolation to have a damping effect
  float targetAngle = Mathf.LerpAngle(currentAngle, wantedAngle, rotationDamping * Time.fixedDeltaTime);
  float targetHeight = Mathf.Lerp(camTransform.position.y, wantedHeight, heightDamping * Time.fixedDeltaTime);

  //set the camera transform where it should be
  Quaternion currentRotation = Quaternion.Euler(0, targetAngle, 0);
  Vector3 wantedPos = targetTransform.position - (currentRotation * Vector3.forward * distance);
  wantedPos.y = targetHeight;
  camTransform.position = wantedPos;
  camTransform.LookAt(targetTransform);
}

Hope it helps somebody.