提问人:Adrian Nugroho 提问时间:6/6/2023 最后编辑:NifimAdrian Nugroho 更新时间:6/7/2023 访问量:239
Lua 错误:参数 #1 无效为“new”(预期为 Vector3,得到 Vector2)
Lua error : invalid argument #1 to 'new' (Vector3 expected, got Vector2)
问:
我想做一个像skriblio一样的游戏。所以我创建了 gui 并尝试连接它们。并编写逻辑代码 尝试运行脚本时出现错误 我的脚本有问题吗?错误在第 12 行 这是我的代码
local UserInputService = game:GetService("UserInputService")
local currentTool = "Line" -- Ganti dengan alat default Anda
local currentColor = Color3.new(0, 0, 0) -- Ganti dengan warna default Anda
local canvas = script.Parent -- Ganti dengan objek "canvas" yang sesuai
local function drawLine(startPoint, endPoint)
local line = Instance.new("Part")
line.Anchored = true
line.Material = Enum.Material.SmoothPlastic
line.BrickColor = BrickColor.new(currentColor)
line.Size = Vector3.new((startPoint - endPoint).Magnitude, 0.2, 0.2)
line.CFrame = CFrame.new((startPoint + endPoint) / 2, Vector3.new(endPoint.X, endPoint.Y, 0))
line.Parent = canvas
end
local function drawCircle(center, radius)
local circle = Instance.new("Part")
circle.Anchored = true
circle.Material = Enum.Material.SmoothPlastic
circle.BrickColor = BrickColor.new(currentColor)
circle.Shape = Enum.PartType.Cylinder
circle.Size = Vector3.new(radius * 2, 0.2, radius * 2)
circle.CFrame = CFrame.new(center) + Vector3.new(0, 0.1, 0)
circle.Parent = canvas
end
local function drawFreehand(startPoint, endPoint)
local line = Instance.new("Part")
line.Anchored = true
line.Material = Enum.Material.SmoothPlastic
line.BrickColor = BrickColor.new(currentColor)
line.Size = Vector3.new(0.2, 0.2, (startPoint - endPoint).Magnitude)
line.CFrame = CFrame.new((startPoint + endPoint) / 2, Vector3.new(endPoint.X, endPoint.Y, 0))
line.Parent = canvas
end
local function erase(startPoint, endPoint)
-- Dapatkan semua objek anak di dalam "canvas"
local canvasChildren = canvas:GetChildren()
-- Iterasi melalui semua objek anak dan periksa apakah mereka berada dalam wilayah yang ditentukan
for _, child in ipairs(canvasChildren) do
if child:IsA("BasePart") then
local position = child.Position
if position.X >= startPoint.X and position.X <= endPoint.X and
position.Y >= startPoint.Y and position.Y <= endPoint.Y then
child:Destroy()
end
end
end
end
UserInputService.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
local startPoint = UserInputService:GetMouseLocation()
if currentTool == "Line" then
-- Menggambar garis
UserInputService.InputEnded:Wait()
local endPoint = UserInputService:GetMouseLocation()
drawLine(startPoint, endPoint)
elseif currentTool == "Circle" then
-- Menggambar lingkaran
UserInputService.InputEnded:Wait()
local endPoint = UserInputService:GetMouseLocation()
local radius = (startPoint - endPoint).Magnitude
drawCircle(startPoint, radius)
elseif currentTool == "Pen" then
-- Menggambar secara bebas
local prevPoint = startPoint
repeat
local currentPoint = UserInputService:GetMouseLocation()
drawFreehand(prevPoint, currentPoint)
prevPoint = currentPoint
wait()
until not UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1)
elseif currentTool == "Eraser" then
-- Menghapus
UserInputService.InputEnded:Wait()
local endPoint = UserInputService:GetMouseLocation()
erase(startPoint, endPoint)
end
end
end)
我的代码知识非常有限,因为我还在学习,我的问题是什么,我需要改变什么?
答:
0赞
Kylaaa
6/7/2023
#1
错误告诉你到底出了什么问题。当您尝试创建 CFrame(一个表示空间中的 3D 位置和方向的对象)时,您为其指定了一个 Vector2,而构造函数需要一个 Vector3。
在函数中,通过平均 startPoint 和 endPoint 变量来构造 CFrame,但这两个变量都来自返回 Vector2 的 UserInputService:GetMouseLocation()。drawLine
一个简单的答案可能是尝试将 Vector2 转换为 Vector3,使用与其他 Vector3 相同的系统。
local linePos = (startPoint + endPoint) / 2
line.CFrame = CFrame.new(Vector3.new(linePos.X, linePos.Y, 0), Vector3.new(endPoint.X, endPoint.Y, 0))
但是,您有 2D 屏幕坐标,并且您正在尝试使用它们将对象放置在 3D 空间中。一种方法是将光线投射到空间中并尝试找到它击中的物体。这里建议使用大块作为画布。因此,尝试使用 Mouse 类,而不是 UserInputService:GetMouseLocation()。它带有一个属性,该属性将为您提供工作区内碰撞点的 CFrame。您可以使用它来获取 Vector3。Hit
UserInputService.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
local mouse = game.Players.LocalPlayer:GetMouse()
local startPoint = mouse.Hit.p
if currentTool == "Line" then
-- Menggambar garis
UserInputService.InputEnded:Wait()
local endPoint = mouse.Hit.p
drawLine(startPoint, endPoint)
elseif currentTool == "Circle" then
-- Menggambar lingkaran
UserInputService.InputEnded:Wait()
local endPoint = mouse.Hit.p
local radius = (startPoint - endPoint).Magnitude
drawCircle(startPoint, radius)
elseif currentTool == "Pen" then
-- Menggambar secara bebas
local prevPoint = startPoint
repeat
local currentPoint = mouse.Hit.p
drawFreehand(prevPoint, currentPoint)
prevPoint = currentPoint
wait()
until not UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1)
elseif currentTool == "Eraser" then
-- Menghapus
UserInputService.InputEnded:Wait()
local endPoint = mouse.Hit.p
erase(startPoint, endPoint)
end
end
end)
评论