Script attached to different objects always return same terrain height on different locations

I have attached a script that creates a building object with a model and a collider on different spots of the terrain in runtime.

The buldings are created, but I stilll can’t get them to be at the same height as the terrain they have underneath. The terrain is not flat, so they should each have different height. However, all buildings end up with the same height, so some are floating and some are halfway undergound.

The script that I have attached to each building is the following:

    private float houseHeight;
    GameObject house;
    RaycastHit hit;

    void HouseHeight()
    {

        house = gameObject;
        Debug.Log("building: " + house.name);
        Vector3 groundzero = new Vector3(house.transform.position.x, 2000f, house.transform.position.z);
        Ray downRay = new Ray(groundzero, Vector3.down);
        if (Physics.Raycast(downRay, out hit))
        {
            houseHeight = 2000f - hit.distance;
            house.transform.position = new Vector3(house.transform.position.x, houseHeight, house.transform.position.z);
            Debug.Log("houseHeight: " + houseHeight);
            Debug.Log("hit.distance: " + hit.distance);
        }

I added the Logs so I would know what was happening. It seems the building name is changing, so each script knows it’s attacked to a different object. But hit.distance is always the same, and houseHeight too. What am I missing?

Thanks for your help.

Try using hit.point.y for the height! RaycastHit.point is the Vector3 location in world position of the RaycastHit.

Something you might want to check if this doesn’t work:
Is the terrain collider set up properly?

Hope this helps!

-Gameplay4all

I think I found the answer, it has something to do with the pivot of the meshrender, that is not located at the center of the object (which is 0,0 on my Project).

This is the code:

private float houseHeight;
    GameObject house;
    RaycastHit hit;
    GameObject model;
    


    void HouseHeight()
    {
        model = transform.Find("model 1").gameObject;
        MeshRenderer renderer = model.GetComponent<MeshRenderer>();
        Vector3 centro = renderer.bounds.center;
        house = gameObject;            
        Vector3 fromtop2 = new Vector3(centro.x, 9000f, centro.z);
        Ray downRay = new Ray(fromtop2, Vector3.down);
        if (Physics.Raycast(downRay, out hit))
        {            
            houseHeight = hit.point.y;
            house.transform.position = new Vector3(house.transform.position.x, houseHeight, house.transform.position.z);

            Debug.Log("building: " + house.name);
            Debug.Log("houseposition: " + house.transform.position.x + " " + house.transform.position.y + " " + house.transform.position.z);
            Debug.Log("houseHeight: " + houseHeight);
            Debug.Log("hit.distance: " + hit.distance);
            Debug.Log("hit.point.y: " + hit.point.y);
            Debug.Log("render center: " + centro.x + " " + centro.y + " " + centro.z);
        }