Why does my Raycast hit through my object?

I’m trying to make a simple Point & Click movement system with raycasting, but whenever I click on the baseplate object, the raycast casts through the object and hits completely something else. I’m still new to Unity3D, and I have no idea why it’s doing this. I tried raycasting only to the baseplate’s layermask, but it didn’t solve the issue.

On the image attachments, the first image is before clicking the baseplate, and the second image is after clicking the baseplate (notice the position.)

If anybody could help me, that would be great!

Here’s the code:

    public Camera cam;

    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("Script running...");
    }

    // Update is called once per frame
    void Update()
    {

        if(Input.GetMouseButtonUp(0)) {
            Ray ray = cam.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if(Physics.Raycast(ray, out hit, Mathf.Infinity, LayerMask.GetMask("Baseplate"))) {
                Vector3 mousePos = Input.mousePosition;
                transform.position = transform.position + new Vector3(mousePos.x, -1.44f, mousePos.z);
            }
        }

    }


I’m guessing you wanted to set the transform’s position to where you clicked? The Raycast is working fine, you just added the mouse position to an object in the 3D world for some reason.

What you probably wanted to do is to change the line which sets the position to this

transform.position = hit.point;