Each game object that you want the camera to be moved to must be set with a tag. I used Player as I dont have anything else in my scene.
I created a GameController object and attached a script to it
In the script I use a Raycast to check if the clicked object has a tag of player. If it does, move the camera to that position + an offset.
using UnityEngine;
public class GameController : MonoBehaviour
{
public Camera cam;
public Vector3 offset;
Transform target;
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Debug.Log("Click");
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out RaycastHit hitInfo))
{
if(hitInfo.collider.tag == "Player")
{
Debug.Log("Hit a player yo");
target = hitInfo.transform;
cam.transform.position = target.position + offset;
}
}
}
}
}