Making object follow floor

Hi i have a placement script that spawns a block where the reticule is eg a transparent cube and i wasn’t that transparent tube to follow the ground which can be anything from rocks to sheep i need it to stick to it and not float in the air like it currently does any idea how i can achieve this?

var player : Transform;
var ActualPlacedPeice : Transform;
function Start() 
{
     player = GameObject.Find("Player").transform;
}
function Update () 
{
	var pos = player.position + player.forward * 5.0;  
    transform.position = pos + Vector3.up;// * 0.5;  
    transform.rotation = Quaternion.LookRotation(player.transform.forward);  
    
    if (Input.GetButtonDown("Fire1")) 
    {
      var Build = Instantiate(ActualPlacedPeice,transform.position, transform.rotation); 
    }
    
}

i used to use this
pos.y = Terrain.activeTerrain.SampleHeight(pos);
but i am nolonger ussing unitys terrain

For an arbitrary mesh, you will need to Raycast() downward.

var hit : RaycastHit;
if (Raycast(transform.position, Vector3.down, hit)) {
    transform.position = hit.point + Vector3.up * halfHeight;
}

‘halfHeight’ is the distance from the bottom of your object to the pivot point. For this kind of thing, it is often best to place the pivot point at the bottom of the object.

A simple way to do this is to raycast directly down, and extract the Y coordinate from the hitpoint. I am assuming the “reticule” is the player in your script? Here’s an example in C#:

Ray ray = new Ray(player.transform.position, Vector3.down);
RaycastHit rayHit = new RaycastHit();
if(Physics.Raycast(ray, out rayHit)){
height = rayHit.point.y;
}