介绍¶
在本教程中,我们将制作一个简单的方块枪支。这个教程需要对以下主题有基本了解:
如果您还没有掌握这些主题,强烈建议在继续之前先学习它们。
制作工具¶
首先,我们将制作一个作为枪支的工具。由于本教程仅涵盖武器的基础知识,我们不会过于强调枪支的形状。
在 Workspace 中添加一个零件,并将其命名为 Handle。

根据玩家角色的手部大小调整零件的大小。

现在在 Workspace 中添加一个 Tool,并将 Handle 的父级设置为该工具。我们的资源管理器层次结构现在如下所示:

如果您想进行测试,请将您的工具放置在 StarterPack 中。

点击播放按钮(或按下 F5)。

设置¶
现在我们将设置我们的武器需要的一切。
在工具中添加一个 Local Script。 在 Replicated Storage 中添加一个 Remote Event。 在 ServerScriptService 中添加一个 Script。

客户端¶
在客户端(本地脚本)上,我们将检查工具何时被使用,并请求服务器发射子弹。首先,我们将把所有需要的东西存储在变量中,以使代码看起来更整洁。
local gun = script.Parent -- 我们的工具
local remote_event = game.ReplicatedStorage.RemoteEvent
local mouse = game.Players.LocalPlayer:GetMouse() -- 获取玩家的鼠标
我们将创建一个名为 shooter 的函数。这个函数将负责在使用工具时进行请求。这将通过在服务器上触发远程事件来完成。这个远程事件还将携带玩家鼠标指向的位置。
local gun = script.Parent -- 我们的工具
local remote_event = game.ReplicatedStorage.RemoteEvent -- 远程事件
local mouse = game.Players.LocalPlayer:GetMouse() -- 获取玩家的鼠标
local gun_connection -- 缓存连接的变量
local function shooter()
local mouse_pos = mouse.Hit.Position -- 鼠标指向的3D世界位置。
local origin = gun.Handle.Position
remote_event:FireServer(mouse_pos, origin)
end
现在我们将连接一个函数到工具的 Equipped 事件。在这个函数中,我们将连接 shooter 函数到工具的 Activated 事件。这个连接将被缓存在变量 gun_connection 中,以便我们在工具被取消配备时断开它(Unequipped 事件被触发)。
local gun = script.Parent -- 我们的工具
local remote_event = game.ReplicatedStorage.RemoteEvent -- 远程事件
local mouse = game.Players.LocalPlayer:GetMouse() -- 获取玩家的鼠标
local gun_connection -- 缓存连接的变量
local function shooter()
local mouse_pos = mouse.Hit.Position -- 鼠标指向的3D世界位置。
local origin = gun.Handle.Position -- 子弹的起点
remote_event:FireServer(mouse_pos, origin)
end
gun.Equipped:Connect(function()
gun_connection = gun.Activated:Connect(shooter)
end)
gun.Unequipped:Connect(function()
gun_connection:Disconnect()
end)
恭喜!我们的客户端部分完成。每次装备工具时,都会建立在工具的 Activated 事件上的连接,并在玩家点击/激活工具时调用 shooter。
服务器端¶
在服务器端,我们需要完成以下任务:
- 创建子弹。
- 设置速度约束。
- 计算方向向量。
- 处理伤害。
- 销毁子弹。
首先,我们必须检查远程事件何时被触发。
创建子弹¶
您可以创建任何大小、颜色和形状的子弹。我将创建方块形状的霓虹黄色子弹。
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, mouse_pos)
-- 创建子弹
local bullet = Instance.new("Part")
bullet.Color = Color3.new(1, 1, 0.133333)
bullet.Size = Vector3.new(1,1,1)
end)
设置速度约束¶
我们将使用 线性速度 来对子弹施加持续速度。为此,我们必须创建一个附件并将其父级设置为子弹。然后,我们将创建一个 LinearVelocity 约束并将附件分配给 LinearVelocity 的 Attachment1 属性。它也将被设置为子弹的父级。
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, mouse_pos)
-- 创建子弹
local bullet = Instance.new("Part")
bullet.Color = Color3.new(1, 1, 0.133333)
bullet.Size = Vector3.new(1,1,1
)
bullet.CanCollide = false
-- 创建附件
local attachment = Instance.new("Attachment")
attachment.Parent = bullet
-- 创建速度约束
local velocity = Instance.new("LinearVelocity")
velocity.Attachment0 = attachment
velocity.Parent = bullet
end)
计算方向向量¶
线性速度需要一个 Vector3 值,指定速度的方向。我们需要一个向量,起始点是工具,指向玩家鼠标指向的方向。
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, mouse_pos, origin)
-- 创建子弹
local bullet = Instance.new("Part")
bullet.Color = Color3.new(1, 1, 0.133333)
bullet.Size = Vector3.new(1,1,1)
bullet.CanCollide = false
-- 创建附件
local attachment = Instance.new("Attachment")
attachment.Parent = bullet
-- 创建速度约束
local velocity = Instance.new("LinearVelocity")
velocity.Attachment0 = attachment
velocity.Parent = bullet
-- 设置速度方向
local speed = 50 -- 子弹速度/向量长度
velocity.MaxForce = 9e6 -- 将最大力设置为 9 的指数 6
local directional_vector = (mouse_pos - origin).Unit * speed
velocity.VectorVelocity = directional_vector
end)
处理伤害¶
这是枪支机制的另一个重要部分。我们将使用 Touched 事件,当子弹触碰到任何对象时,我们将检查其父级并搜索一个 humanoid。如果成功找到 humanoid,则我们可以开始造成伤害。
警告
确保不要伤害射手的 humanoid。
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, mouse_pos, origin)
-- 创建子弹
local bullet = Instance.new("Part")
bullet.Color = Color3.new(1, 1, 0.133333)
bullet.Size = Vector3.new(1,1,1)
bullet.CanCollide = false
-- 创建附件
local attachment = Instance.new("Attachment")
attachment.Parent = bullet
-- 创建速度约束
local velocity = Instance.new("LinearVelocity")
velocity.Attachment0 = attachment
velocity.Parent = bullet
-- 设置速度方向
local speed = 50 -- 子弹速度/向量长度
velocity.MaxForce = 9e6 -- 将最大力设置为 9 的指数 9
local directional_vector = (mouse_pos - origin).Unit * speed
velocity.VectorVelocity = directional_vector
--造成伤害
bullet.Touched:Connect(function(hit)
if hit.Parent ~= player.Character and hit.Parent:FindFirstChild("Humanoid") then
hit.Parent.Humanoid.Health -= 15
bullet:Destroy()
end
end)
bullet.Parent = workspace
bullet.Position = origin
end)
销毁子弹¶
最后,我们将在几秒钟后通过碎片服务销毁子弹。
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, mouse_pos, origin)
--创建子弹
local bullet = Instance.new("Part")
bullet.Color = Color3.new(1, 1, 0.133333)
bullet.Size = Vector3.new(1,1,1)
bullet.CanCollide = false
--创建附件
local attachment = Instance.new("Attachment")
attachment.Parent = bullet
--创建速度约束
local velocity = Instance.new("LinearVelocity")
velocity.Attachment0 = attachment
velocity.Parent = bullet
--设置速度方向
local speed = 50 -- 子弹速度/向量长度
velocity.MaxForce = 9e6 --将最大力设置为九的指数 9
local directional_vector = (mouse_pos - origin).Unit * speed
velocity.VectorVelocity = directional_vector
--造成伤害
bullet.Touched:Connect(function(hit)
if hit.Parent ~= player.Character and hit.Parent:FindFirstChild("Humanoid") then
hit.Parent.Humanoid.Health -= 15
bullet:Destroy()
end
end)
bullet.Position = origin
bullet.Parent = workspace
game.Debris:AddItem(bullet, 8)
end)

结语¶
希望这个教程对您有帮助,并让您了解如何设计射击武器。您可以通过更改子弹、射击样式等来进行大量自定义。非常感谢阅读!再见! ```