This code basically checks the part below the player (the floor) and then I basically position an item based on the floors height. With the prints, you can the exact number, plus the rounded out number (being 10) However you can that my model is being placed slightly above the ground (I believe it’s 0.5 studs?) Is there something I’m missing here?
local CheckForFloor = Ray.new(Character.HumanoidRootPart.Position, Vector3.new(0, -5, 0))
local Floor, FloorPos = workspace:FindPartOnRayWithWhitelist(CheckForFloor, {PlayersInterior.Build.InteriorElements})
print(FloorPos.Y)
print(math.floor((FloorPos.Y / 0.5) + 0.5) * 0.5)
local ClonedItem = item:Clone()
-- Snap to a 0.5 grid
local PosX = math.floor((Pos.X / 0.5) + 0.5) * 0.5
local PosY = math.floor((FloorPos.Y / 0.5) + 0.5) * 0.5
local PosZ = math.floor((Pos.Z / 0.5) + 0.5) * 0.5
local NewPos = Vector3.new(PosX, PosY, PosZ)
-- Place object on client (visual aid, so there's no 2-3 second wait time to reach the server)
ClonedItem:SetPrimaryPartCFrame(
CFrame.new(NewPos) *
CFrame.Angles(0, math.rad(90), 0) *
CFrame.new(0, ClonedItem.PrimaryPart.Size.Y / 2, 0)
)
ClonedItem.Parent = PlayersInterior.Build.Furniture
-- Place on the server
Place:InvokeServer(item.Name, ClonedItem.PrimaryPart.CFrame)
-- Delete the duplicated item
ClonedItem:Destroy()
Another problem I face is if I place the item while in mid air, it also places said item in the air. I want the item to always spawn at the floors height.
This is the output for the midair object
[11.536449432373]
[11.5]
Please note, I didn’t include the Pos.X and Pos.Z calculations, as they are based on a separate ray and have nothing to do with my problem, I’m purely just looking at the Y axis.
You’re snapping to a grid - the place on the floor you want to place the object on isn’t perfectly aligned with the grid, instead the grid starts a bit above it. You should adjust where the grid is exactly. 12 hours later I am contemplating on what the hell I was thinking when I wrote this - apologies!
Maybe you could try finding the bottom parts/ends of the model via GetBoundingBox which allows you to find the “hitbox” of the model.
Use that and find the approximate bottom of the model, then using that, move it down til the bottom of the model touches the grid’s Y axis/pos essentially making it always touch the bottom perfectly no matter the situation.
You can of course enable/disable based on model type for picture frames/etc. You can also try offsetting it if it doesn’t look right
You would do this of course after the snapping of 0.5 studs above and placing it so that it works properly and doesn’t interfere.
local CheckForFloor = Ray.new(Character.HumanoidRootPart.Position, Vector3.new(0, -20, 0))
local Floor, FloorPos = workspace:FindPartOnRayWithWhitelist(CheckForFloor, {PlayersInterior.Build.InteriorElements})
local ClonedItem = item:Clone()
-- Snap to a 0.5 grid
local PosX = math.floor((Pos.X / 0.5) + 0.5) * 0.5
local PosY = math.floor((FloorPos.Y / 0.5) + 0.5) * 0.5
local PosZ = math.floor((Pos.Z / 0.5) + 0.5) * 0.5
local NewPos = Vector3.new(PosX, PosY, PosZ)
print(PosY)
print(ClonedItem.PrimaryPart.Size.Y / 2)
-- Place object on client (visual aid, so there's no 2-3 second wait time to reach the server)
ClonedItem:SetPrimaryPartCFrame(
CFrame.new(NewPos) *
CFrame.Angles(0, math.rad(90), 0) *
CFrame.new(0, ClonedItem.PrimaryPart.Size.Y / 2, 0)
)
I stil however am getting a problem, where it’s always 0.25 studs off the ground, and I don’t know why. I’m sure I could do the position -0.25, but I fear that may cause problems further down the road with other items and different hitboxes
Why don’t you try creating multiple complex models (no need to go detailed of course), different hitbox variations, and try seeing if it still messes up with a 0.25 stud difference. If so, I think it’ll be find to offset it by -0.25 to make it work. If it varies however, you may want to check it again.