Unity Glitch for Creating Primitives via RayHit info?

Hi,I have code that uses a RayCast and creates a primitive object at the location. To create the primitive I set the position of it to (x, rayOrigin.y - hit.distance, z). Theoretically this should work fine. When I run the code all the squares I create seem to be in the right place visually, but, when I click on them, the inspector reports the x and z values correctly but the y value is completely off!

The y coordinate for this one is -2.2053 (but its actually at 0)

The y coordinate for this one is -7.1525 (but it’s actually at 0 as well!)

Something that I checked before posting is whether the model might be rotated, and it’s not, its right on the x and z axis, and perpendicular at all points to the y axis, therefore there should not be any changes in height due to rotation. In addition in other points of the map where the height of the floor is not 0, the height of the created object is correct (uh what?).

I’m worried it might cause miscalculations, say, if I wanted to store this height and use it later to create an object or vertex. Is this a known glitch, or is it something I didn’t take account for?

P.S: I don’t know if this plays a role but the floor is a flat grid made on blender with some areas of extrusion (areas shown in pic are flat and not extruded).

The two things (raycast and spawning) aren’t connected. Break it into parts. One part has the problem.

Raycasts hit colliders where they hit, and tell you where. It doesn’t matter what you later want to use the hit point for. Either there’s a problem with your Raycast, or you aren’t reading RayHit correctly, or there’s a problem with how you place the objects. Check the usual “raycast hitting in wrong spot” questions. Test to see the point is correct. If that’s all working, check the “object not placed at correct location” questions.

But, why not just use hit.point? Your distance formula is only correct if the ray is always aimed straight down (but hit.point is still easier to use.)