Object won't place correctly on the right Y axis

So I made a game with an isometric view (top-view,3d view) and I added a placement feature which should place structures. The object does follow the cursor however, It’s not in the right level of terrain.

Below is what I’m trying to show, The object follows the cursor where it should be placed and then upon clicking, the object should spawn there. I configured the code so that the structure will spawn on the right Y axis level, however, It’s too far on the cursor making it hard to see.
Cursor12
Code below,

			local OBJRay,pos =  workspace:FindPartOnRayWithIgnoreList(RayCast)

					
					local ScreenRay = workspace.CurrentCamera:ScreenPointToRay(mouse.X,mouse.Y, 1)
					local hitpos = ScreenRay.Origin + ScreenRay.Direction * 500
					
					local newCFrame = CFrame.new(hitpos.X,1,hitpos.Z) -- This is the line that configures where the structure will spawn, as you can see, I made the Y axis 1 so it spawns just above the terrain, However It's too far from the cursor.
					
					newStructurePlaceH:SetPrimaryPartCFrame(newCFrame)									
				end)

Second picture, This is what I’ve wanted to do, The object is just right near the cursor however, the object is not in the right axis and is too far from the ground.

local newCFrame = CFrame.new(hitpos.X,1,hitpos.Z) -- This is the line that configures where the structure will spawn, as you can see, I made the Y axis follow the Y axis of mouse however, the position of object is too far from the ground

Alright, I’m stumped. I don’t know how to do this. I have a feel for what you’re trying to do, but I can’t factor out how it would work mathematically. Here’s what I got, which seems fairly close but the logic for using the mouse’s Y component is off:

https://gyazo.com/47aec0f36421287c652e4bf89c10e498.mp4

attempt at ground placement.rbxl (30.8 KB)

1 Like

When you calculate hitpos in your code snippet, you are creating a line that starts at the ray origin and is always 500 units long. This gives you no guarantee about whether that line ends exactly on your terrain surface. Did you mean to use the result of a raycast?:

local target = workspace:Raycast(ScreenRay.Origin, ScreenRay.Direction * maxSelectDistance, rayparams)
	
--Cancel if ray hits nothing
if target == nil then return end
local hitpos = target.Position