Rig is not at correct position when placing

In the code below I’m trying to make it so the position of the rig so it is is where the mouse is on the baseplate, but it keeps going under the baseplate.

What I’m trying to do is place it above the baseplate.

Below is the code I am using:

local function ifCanPlace(self, player, name)
	local camera = workspace.CurrentCamera
	local mouse = player:GetMouse()

	local connection
	local towerExists = ReplicatedStorage.Units:FindFirstChild(name)
	if not towerExists then
		if connection then
			connection:Disconnect()
		end
		return
	end

	local towerClone = towerExists:Clone()
	towerClone.Parent = workspace

	if not towerClone.PrimaryPart then
		towerClone.PrimaryPart = towerClone:FindFirstChild("HumanoidRootPart") or towerClone:FindFirstChildWhichIsA("BasePart")
	end

	local function updateTowerPosition()
		local mousePosition = UserInputService:GetMouseLocation()
		local mouseRay = camera:ViewportPointToRay(mousePosition.X, mousePosition.Y)
		local raycastParams = RaycastParams.new()
		raycastParams.FilterType = Enum.RaycastFilterType.Exclude
		raycastParams.FilterDescendantsInstances = {towerClone}

		local raycastResult = workspace:Raycast(mouseRay.Origin, mouseRay.Direction * 1000, raycastParams)

		if raycastResult and raycastResult.Instance then
			local x = raycastResult.Position.X
			local y = raycastResult.Position.Y + towerClone.Humanoid.HipHeight
			local z = raycastResult.Position.Z

			local cframe = CFrame.new(x, y, z)
			towerClone:SetPrimaryPartCFrame(cframe)
		end
	end

	connection = RunService.RenderStepped:Connect(function(deltaTime)
		if towerExists then
			updateTowerPosition()
		end
	end)
end

Proof:

1 Like

you can try adding the character’s size Y value to the final cframe

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.