Have object follow mouse on terrain

I’m having difficulties with having an object follow the mouse, but on the terrains position. The Raycast is returning true, but I’m not seeing the game object appear anywhere.

I’m getting the “Terrain hit!” in the log.

using UnityEngine;
using System.Collections;

public class PlaceItem : MonoBehaviour {

    public bool PlacingItem;

    Camera MainCamera;

    Vector3 MousePos;
	// Use this for initialization
	void Start () {
      //  PlacingItem = false;
        MainCamera = Camera.main;
	// Update is called once per frame
    void Update()

        if (PlacingItem)
            Ray ray;
            RaycastHit hit;

            //Always updating the mouse position.
            MousePos = Input.mousePosition;
            ray  = Camera.main.ScreenPointToRay(MousePos);

            if (Physics.Raycast(ray, out hit) && hit.collider.gameObject.CompareTag("Terrain"))
                Debug.Log("Terrain hit!");
                gameObject.transform.position = hit.transform.position;
    public void StartPlacing()

        PlacingItem = true;
        Debug.Log("Placing Item: " + PlacingItem);


That’s because you’re accessing the location of the transform that was hit, not the actual hit point itself.

So, if I have a transform that is 10 x 10 x 10 units big, and is at coordinates Vector(1, 2, 3), the result of my hit.transform.position will be

if instead, you access hit.point
you’ll get the actual coordinate where the raycast intersects with a collider.
Verify by pausing your game scene with your current code while the raycast is hitting, and go to the zero point of your terrain collider, and you’ll probably find your object.

a RaycastHit does not have a transform of it’s own, so any transform you access through RaycastHit will actually be the transform that was hit by the Raycast.