How would I make a part anchor to the floor?

How would I make a part anchor to the ground like in the video above. I know it probably has something to do with raycast and cframe. Is there anything I can read on this?

2 Likes

Can you please provide your script?

You can read Raycasting | Roblox Creator Documentation and CFrames | Roblox Creator Documentation to try and understand each.

Personally I’d do something like this:

local function GetBasePartAndSize(object) -- returns only one BasePart in an object (if there is one)
	local basePart = if object:IsA("BasePart") then object else if object:IsA("Model") and object.PrimaryPart then object.PrimaryPart else object:FindFirstChildOfClass("BasePart")

	if not basePart then -- if there is not a BasePart search for one recursively
		for _,child in pairs(object:GetChildren()) do
			basePart = if child:IsA("BasePart") then child else child:FindFirstChildOfClass("BasePart")
			if basePart then break end -- if there is a BasePart then quite searching for one
		end
	end

	local size = if object:IsA("Model") then object:GetExtentsSize() else if basePart then object.Size else nil

	return basePart, size
end

local function PutObjectDown(object, positionCf, character)
	if not typeof(object) == "Instance" then return end

	local basePart, size = GetBasePartAndSize(object)
	if basePart then
		local params = RaycastParams.new()
		params.IgnoreWater = true --whether or not the raycast will ignore terrain water
		if character then
			params.FilterDescendantsInstances = {character, object}
		else
			params.FilterDescendantsInstances = {object}
		end

		local result = workspace:Raycast(positionCf.Position, Vector3.new(0, -4, 0), params)
		
		if result then
			positionCf = (positionCf-positionCf.Position) + (result.Position + Vector3.new(0, size.Y*0.5, 0)) -- (num*0.5) is faster than (num/2) -- the cframe to place the bag at

			local moveObject = if object:IsA("PVInstance") then object else basePart
			moveObject:PivotTo(positionCf)
		end
	end
end

An example usage of this might be:

PutObjectDown(Part, Character.HumanoidRootPart.CFrame, Character) -- note: the third 'Character' argument is optional

Just know that this code is just an example for how one might do this. It is not meant to fit every particular situation or setup. Also, the raycast may not find a floor if the player is standing on an edge. I’ve created a function specifically for this, but it’s somewhat advanced (you at least need to understand why it works) and not needed for demonstration purposes. You can, however, respond if you want me to share this function with you. Or dm me. Which ever one works for you.

You could probably do

--drop the box
Local weld = instance.new("WeldConstraint")
weld.Parent = -- path to box
weld.Part0 = -- path to box
weld.Part1 = baseplate