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: