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.
It seems groundzero is always looking at (0, 2000, 0), because the position of the object is always (0,0,0), I guess it's a local position ins$$anonymous$$d of a world position. And suddenly, Unity switches to world position when doing the raycast so it always returns the same distance. I still don't know how to solve it, I will keep looking.
I placed a simple cube at (0,950,0). All buildings now have 950 height because the raycast hits the cube at this height. On the scene the buildings appear to be located at their correct location (not all at the same place), but their transform on runtime is always 0,950,0.
How is it posible to see all buildings placed at different locations but their transform.position.x and transform.position.z is 0?

Answer by davaguco · Mar 22, 2017 at 01:44 PM
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);
}
Answer by gameplay4all · Mar 21, 2017 at 01:26 PM
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 used hit.point.y but still gives the same result. The terrains do have terrain colliders.
Okay, I guess this is happening. I can't be sure because I don't know how your project is setup.
All your building objects have pivot points way off center. Try changing the scene view to show the pivot point ins$$anonymous$$d of the center (press Z) or use the button under the "component" menu in the bar above. That's why the LOCAL position, which is shown in the editor, is 0 and the buildings still seem to have different positions. The position shown in the editor is relative to the "buildings" object you have them parented to. So since transform.position returns the pivot, which is the same for all the buildings and seems to be at (0,0,0), and your raycast is cast from this position it all returns the same height because they are cast from the same position.
Hope this helps!
-Gameplay4all
P.S. Try debugging all the transform.position s :)
Your answer