So for my building system I noticed a problem that my object placement is in the middle instead of being on the baseplate.
The solution I found was to raise the Y Size by 1 to solve this issue. But say I make this test object larger, or smaller, is there a way I could calculate how to raise it above ground, my idea would be the divide the object Y Size by half, and use that value to raise it, but I am unsure if that’s the best solution, help will be greatly appreciated.
Instead of dividing the Y position, divide the Y size by 2.
Here’s a simple function to get the Y offset for a part or model
local function GetYOffset(object)
if object:IsA('BasePart') then
return object.Size.Y/2
elseif object:IsA('Model') then
local _, size = object:GetBoundingBox()
return size.Y/2
end
end
I think you are going to need to build a more complicated system. You should consider if the player wants to add a block to the side of another block, or if you will allow rotation of parts in 3d space. You won’t have one height for every situation.
That’s what I mean, that’s why I as @Shamplify and @Fusionet pointed out to get the Y value by either getting the height of the part and dividing it by 2, or another method is using magnitude to get a formula. I will try both ways to see which one works best.
I’ve been pondering this for a bit. If you can capture the position of a surface impact of a raycast (I don’t know how to do this, unfortunately), the magnitude between that position and the center position is the height at that angle. I like the idea of finding the height without knowing anything about the size or shape of the parts in question. Should be pretty easy from there to have two parts raycast at each other to determine how to line them up.
Anyway, sounds like you have some solutions to explore. Good luck!
Edit: I confirmed RaycastResult.Position is the surface impact point. So the magnitude between that and the part’s position should be the height.
I am personally using Mouse.Hit.P for the Hit Position, and if I understood your method correctly it would need the part to both do a raycast, but I think it would cost less performance just to get the part size, here is what I tried to do for your method, but of course it doesn’t function properly, I am unsure how to implement your method correctly, but I personally think getting size should be easier than using raycasts.
local ObjectY = (TableModel.PrimaryPart.Position-MousePosition).Magnitude
return CFrame.new(MousePosition.X,ObjectY,MousePosition.Z)
Swap the placement system to using a base part that fills the entire object and thats what your moving, it’ll save you from all the offset problems. Heres a example:
Yeah that’s what I am wanting to do, I actually followed that tutorial a little for basic idea of how to make the system, and yeah I will take the half size of that part, and use that Y value to raise the whole Model itself.
Alright so I finally decided how to design my system, so inside each model there is a sort of part that is the exact size of the complete model, which is also the primary part.
I used this simple formula to get the offset:
local YSize = Model.PrimaryPart.Size.Y/2
return CFrame.new(MousePosition.X,MousePosition.Y+YSize,MousePosition.Z)
Hope this helps anyone who also has a similar problem or issue. I personally will be marking @Shamplify as the main solution, as its the closest to the solution I used above. Thank you to everyone who helped.