How can a make the camera follow a rolling ball from behind ?

Starting from the Roll-A-Ball demo project, I want to extend the camera script to follow the player, but also keeping track of the direction of movement.
Ex: If the ball starts moving to the side, I need the camera to rotate around the player’s axis to position behind him, and then start following him.

The original camera script from the example project:

    public GameObject player;

    private Vector3 offset;

    void Start ()
    {
        offset = transform.position - player.transform.position;
    }
    
    void LateUpdate ()
    {
        transform.position = player.transform.position + offset;
    }

One way would be to use the player’s normalized rigidbody’s velocity as the direction, scale it some distance, and rotate it to the correct rotation.

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {
	public GameObject player;
	const float distance=2f;
	// input values for xRot, yRot, and zRot
	Quaternion rotation=Quaternion.Euler(xRot, yRot, zRot);

	void Update () {
		Vector3 direction=player.GetComponent<Rigidbody>().velocity.normalized;
		Vector3 offset= distance*direction*rotation;
		transform.position = player.transform.position + offset;
	}
}