Hello, fellow DevForum users. It appears that I am having an issue with my Room Placement local script, it is yet to be finished, however, there are issues that I cannot figure out how to fix.
Issues:
- Placement has issues when moving the end point to a negative X or Z axis.
- The preview is slightly elevated when placing the end point.
Here is the source code for anyone interested.
local uis = game:GetService("UserInputService")
local replicatedStorage = game:GetService("ReplicatedStorage")
local runs = game:GetService("RunService")
local players = game:GetService("Players")
local camera = workspace.CurrentCamera
local placeBut = script.Parent.Place
local start
local ending
local preview
local isPlacingRoom = false
local region = nil
local player = players.LocalPlayer
local mouse = player:GetMouse()
local function snapToGrid(pos:Vector3)
local x = math.floor(pos.X / 4) * 4
local y = math.floor(pos.Y / 4) * 4
local z = math.floor(pos.Z / 4) * 4
return Vector3.new(x,y,z)
end
placeBut.MouseButton1Click:Connect(function()
if preview == nil and start == nil and ending == nil then
preview = Instance.new("Part")
preview.Transparency = .5
preview.CanCollide = false
preview.Size = Vector3.new(1,8,1)
preview.Anchored = true
preview.BrickColor = BrickColor.new("Bright green")
preview.Parent = camera
preview.Material = Enum.Material.SmoothPlastic
isPlacingRoom = true
end
end)
runs.RenderStepped:Connect(function()
if start == nil and preview ~= nil and isPlacingRoom then
local hit,pos = workspace:FindPartOnRayWithIgnoreList(Ray.new(
mouse.UnitRay.Origin,
mouse.UnitRay.Direction*10000
),
{
player.Character,
preview,
},
true,
true
)
if hit then
local snapped = snapToGrid(pos+Vector3.new(0,preview.Size.Y/2,0)-hit.Position)+hit.Position
preview.CFrame = CFrame.new(preview.Position:Lerp(snapped,.4))*CFrame.Angles(math.rad((preview.Position-snapped).Magnitude),0,0)
end
elseif start ~= nil and ending == nil and isPlacingRoom then
local hit,pos = workspace:FindPartOnRayWithIgnoreList(Ray.new(
mouse.UnitRay.Origin,
mouse.UnitRay.Direction*10000
),
{
player.Character,
preview,
},
true,
true
)
if hit then
local snapped = snapToGrid(pos+Vector3.new(0,preview.Size.Y/2,0)-hit.Position)+hit.Position
if snapped ~= start then
region = Region3.new(start,snapped+Vector3.new(0,8,0))
preview.Size = preview.Size:Lerp(region.Size,.25)
preview.CFrame = preview.CFrame:Lerp(region.CFrame,.25)
end
end
end
end)
uis.InputBegan:Connect(function(iput)
if iput.KeyCode == Enum.KeyCode.Q then
if preview then
preview:Destroy()
preview = nil
isPlacingRoom = false
start = nil
ending = nil
end
end
if iput.UserInputType == Enum.UserInputType.MouseButton1 then
if start == nil and preview ~= nil and isPlacingRoom then
local hit,pos = workspace:FindPartOnRayWithIgnoreList(Ray.new(
mouse.UnitRay.Origin,
mouse.UnitRay.Direction*10000
),
{
player.Character,
preview,
},
true,
true
)
if hit then
local snapped = snapToGrid(pos+Vector3.new(0,preview.Size.Y/2,0)-hit.Position)+hit.Position
start = snapped
end
elseif start ~= nil and ending == nil and isPlacingRoom then
preview:Destroy()
preview = nil
isPlacingRoom = false
start = nil
ending = nil
end
end
end)
Any help is greatly appreciated.