So im trying to make a basic game where you can build freely, but when you rotate a block, the placement calculation code breaks entirely. Please help. urgently as possible.
(The error occured when i made the next cube, and rotated it 90 degrees to the left, this breaks for all directions too. it’s odd.)
Heres a recording, the blocks are 3 by 3 on studs. When i rotate it any direction. it messes up , and the block clips into itself, how do i fix this?
Code:
local Mouse = game.Players.LocalPlayer:GetMouse()
local CF
local Orientation = script:FindFirstChildOfClass("Vector3Value")
local Position
local PreviewCF = CFrame.new()
local Limit = 25
local part
local X
local Y
local Z
local cooldown = false
local TS = game:GetService("TweenService")
local SurfaceVectors = {
Top = Vector3.new(0, 1, 0),
Bottom = Vector3.new(0, -1, 0),
Left = Vector3.new(-1, 0, 0),
Right = Vector3.new(1, 0, 0),
Front = Vector3.new(0, 0, -1),
Back = Vector3.new(0, 0, 1),
}
local GridSize = 3
local UIS = game:GetService("UserInputService")
UIS.InputBegan:Connect(function(input,gp)
if gp then return end
if input.KeyCode == Enum.KeyCode.R then
if Orientation.Value.Y == 360 then
Orientation.Value = Orientation.Value + Vector3.new(0,-270,0)
else
Orientation.Value = Orientation.Value + Vector3.new(0,90,0)
end
end
end)
UIS.InputBegan:Connect(function(input,gp)
if gp then return end
if input.KeyCode == Enum.KeyCode.Y then
if Orientation.Value.Z == -360 then
Orientation.Value = Orientation.Value + Vector3.new(0,0,270)
else
Orientation.Value = Orientation.Value + Vector3.new(0,0, -90)
end
end
end)
UIS.InputBegan:Connect(function(input,gp)
if gp then return end
if input.KeyCode == Enum.KeyCode.T then
if Orientation.Value.X == 360 then
Orientation.Value = Orientation.Value + Vector3.new(-270,0,0)
else
Orientation.Value = Orientation.Value + Vector3.new(90,0,0)
end
end
end)
script.Parent.Activated:Connect(function()
if cooldown == false then
cooldown = true
local distance = (Mouse.Hit.p - script.Parent.Parent.HumanoidRootPart.Position).Magnitude
if distance < Limit then
local Normal = Mouse.TargetSurface
local hitblock = Mouse.Target
script.Parent.place:FireServer(PreviewCF)
end
wait(.05)
cooldown = false
end
end)
game:GetService("RunService").RenderStepped:Connect(function()
local surface = Mouse.TargetSurface.Name
local surfaceVector = SurfaceVectors[surface]
local adjustedX = Mouse.Hit.Position.X + surfaceVector.X
local adjustedY = Mouse.Hit.Position.Y + surfaceVector.Y
local adjustedZ = Mouse.Hit.Position.Z + surfaceVector.Z
print(surface)
local xPos = math.round(adjustedX / GridSize) * GridSize
local yPos = math.round(adjustedY/ GridSize) * GridSize
local zPos = math.round(adjustedZ / GridSize) * GridSize
PreviewCF = CFrame.new(Vector3.new(xPos,yPos,zPos))*CFrame.fromEulerAnglesXYZ(math.rad(Orientation.Value.X),math.rad(Orientation.Value.Y),math.rad(Orientation.Value.Z))
end)
script.Parent.Equipped:Connect(function()
part = game.ReplicatedStorage.Block:Clone()
part.Transparency = .5
part.CanCollide = false
part.Parent = game.Workspace.Camera
Mouse.TargetFilter = part
while wait() do
if part ~= nil then
part.BillboardGui.TextLabel.Text = "("..tostring(Orientation.Value)..")"
TS:Create(part,TweenInfo.new(0.05,Enum.EasingStyle.Linear),{CFrame = PreviewCF}):Play()
end
end
end)
script.Parent.Unequipped:Connect(function()
part:Destroy()
end)