move camera based on mouse click on object

Hello,

I am working on a simple puzzle game but I am horrible with programming. I looked around and was able to figure out a few things about moving the camera but I am having issues with using Raycasting to move the camera when I click on an object.

Here is what I got so far. This script is attached to the object to be clicked on:

Vector3 moveToPosition; // This is where the camera will move after the start
    Vector3 backToPosition; // This is where the camera will move if not in the original position
    float speed = 2f; // this is the speed at which the camera moves
    bool cameraMoved = false; // this checks to see if the camera was moved

    // Start is called before the first frame update
    void Start()
    {
        //Move the camera to this position
        moveToPosition = new Vector3(100, 0, -10);

        //Move the camera back to the starting position
        backToPosition = new Vector3(0, 0, -10);
    }

    // Update is called once per frame
    void Update()
    {
        //Mouse Click on Cube 1
        if (Input.GetMouseButtonDown(0))
        {
            //Create variables to cast a ray on Cube 1
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            //Detect if ray hit Cube 1
            if (Physics.Raycast(ray, out hit, 100))
            {

                //If camera was not moved...
                if (!cameraMoved)
                {
                    // Move the camera into position
                    transform.position = Vector3.Lerp(transform.position, moveToPosition, speed);
                    cameraMoved = true;
                }

                //If camera was moved...
                else
                {
                    // Move the camera back into the starting position
                    transform.position = Vector3.Lerp(transform.position, backToPosition, speed);
                    cameraMoved = false;
                }
            }
        }
    }

What am I missing? Thanks!

Unless I misunderstand the question your Script worked pretty well. This is how I adapted your code

public class CameraMoveOnClick : MonoBehaviour
{
                                  
    Vector3 startPosition = new Vector3(0, 0, -10); // This is where the camera will start
    Vector3 movedPosition = new Vector3(10, 0, -10); // This is where the camera will move when object is clicked
    bool cameraMoved = false; // this checks to see if the camera was moved
    [SerializeField] private LayerMask objectLayer;

    // Start is called before the first frame update
    void Start()
    {
        //Move the camera to starting position
        transform.position = startPosition;
        
    }

    // Update is called once per frame
    void Update()
    {
        //Mouse Click on Cube 1
        if (Input.GetMouseButtonDown(0))
        {
            //Create variables to cast a ray on Cube 1
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            //Detect if ray hit Cube 1
            if (Physics.Raycast(ray, out hit, 100, objectLayer))
            {

                //If camera was not moved...
                if (!cameraMoved)
                {
                    // Move the camera into position
                    transform.position = movedPosition;
                    cameraMoved = true;
                }

                //If camera was moved...
                else
                {
                    // Move the camera back into the starting position
                    transform.position = startPosition;
                    cameraMoved = false;
                }
            }
        }
    }
}

when the object with the layer “object” is clicked the camera will move 10 units positive in the x axis, when clicked again the camera will move back. This script needs to be assigned to the camera and not the object and in the inspector you need to add the layer “object” and assign that layer to the object you are clicking.