move Camera position to center of the selected game object at some distance?

I am developing an application in that my objective is when i click the gameobect the camera moves towards the object and focus at the center.

here are the code snippet i have added

Vector3 relativePosition = target.position - controller.transform.position;
controller.targetRotation = Quaternion.LookRotation( relativePosition );

Thanks,
jegatheeshmoorthy

Hey @jegatheeshmoorthy

This is how I did it.

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;
                }
            }
        }
    }
}