Hello everyone,
I’m trying to make a placement system of sorts, but I need the object being placed to always stay at the same Y level. Here’s a video of the system right now:
As you can see, the object being placed is able to freely move at any Y level (I placed an invisible part to demonstrate this). However, I would like the object to always stay at the Y level of the invisible part. How would I modify this LocalScript:
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local UIS = game:GetService("UserInputService")
local Part = script:FindFirstChild("Part")
Part.Parent = workspace
local function Raycast(X, Y)
local Cam = workspace.CurrentCamera
local Ray = Cam:ScreenPointToRay(X, Y)
local Params = RaycastParams.new()
Params.FilterType = Enum.RaycastFilterType.Exclude
Params.FilterDescendantsInstances = {Part}
local Raycast = workspace:Raycast(Ray.Origin, Ray.Direction * 500, Params)
return Raycast.Position
end
local function snap()
local detect = workspace:WaitForChild("detect")
print((Part.Position - detect.Position).Magnitude)
if (Part.Position - detect.Position).Magnitude <= 0.5 then
print("snap")
Part.Position = detect.Position
else
local function clampVector(size: Vector3, vector: Vector3)
return Vector3.new(
math.clamp(vector.X, -size.X / 2, size.X / 2),
vector.Y,
math.clamp(vector.Z, -size.Z / 2, size.Z / 2)
)
end
local plot = workspace:WaitForChild("detect"):WaitForChild("area")
local mouseCFrameRelativeToPlot = plot.CFrame:ToObjectSpace(mouse.Hit)
local clampedPosition = clampVector(plot.Size, mouseCFrameRelativeToPlot.Position)
local finalCFrame = plot.CFrame:ToWorldSpace(CFrame.new(clampedPosition.X, clampedPosition.Y, clampedPosition.Z))
Part.CFrame = finalCFrame
end
end
UIS.InputBegan:Connect(function(input, processed)
if input.UserInputType == Enum.UserInputType.MouseMovement then
local Pos = Raycast(input.Position.X, input.Position.Y)
Part.Position = Pos
snap()
end
end)
UIS.InputChanged:Connect(function(input, processed)
if input.UserInputType == Enum.UserInputType.MouseMovement then
local Pos = Raycast(input.Position.X, input.Position.Y)
Part.Position = Pos
snap()
end
end)
in order to keep the object at the same Y level?
Thanks for any help.