Trouble getting a "Ghost" object to appear on the ground using raycast.

Hi guys, need a little help with this one if I may.

In the project I’m currently working on, all I’m trying to achieve is this.

You can buy turrets, select them, and place them.

When you place them a ghost object is shown, clicking will instantiate the prebab of the actual object.

My issue at the moment is that the ghost object doesn’t show where I’m looking, and seems to snap to the centre of certain objects, rather than the specific point I’m looking.

Here’s an example: Screen Recording 2023-05-07 at 12.40.52 AM

I’m at a loss, I’m sure it’s something stupid and simple, below is the code responsible for this part of the system.

void PlaceTurret()
    {
        RaycastHit hit;

        if(Physics.Raycast(mainCamera.transform.position, mainCamera.transform.forward, out hit, Mathf.Infinity))
        {
            ghostObject.transform.position = hit.transform.position;
            Debug.DrawRay(mainCamera.transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.red);
        }
    }

Edit: I’m calling PlaceTurret(); in Update, I’ve tried running this in update itself also, I do have another snippet of code to check if I’m looking at the vending machine which is responsible for the whole buying a crate from the vending machine thing. Here is the code for that part that works flawlessly:

RaycastHit hit;
        if (Physics.Raycast(mainCamera.transform.position, mainCamera.transform.forward, out hit, interactionDistance))
        {       
            if (hit.collider.gameObject.tag == "Vend")
            {
                isLookingAtMachine = true;
                promptText.enabled = true;
            }
            else
            {
                isLookingAtMachine = false;
                promptText.enabled = false;
            }else
        {
            isLookingAtMachine = false;
            promptText.enabled = false;
        }
        }

Even though my profile says I’ve had an account since 2011, I have ADHD and this is a hyper focus of mine, that I go hard with for a few months before moving onto something else for a couple of years before coming back and forgetting everything I know. This is the beginning of probably the 10th cycle

You’ll need your screen point to ray.

There is a unity function for it

Camera Screen Point to Ray a quick Google will get the top result in the docs.

Otherwise you only ever ray center of camera forward.

I realised what I was doing wrong, using hit.transform.position, was centring the object to the transform.position of the ‘hit’ object, I wanted hit.point!

Thanks.