簡素的な飛び道具をツールで作ります。
- StarterPackにToolを入れます。
- Toolの中にPartを入れ、名前をHandleにします。Shape や Size は自由に変更して構いません。
- Tool内にRemoteEventを入れます。名前をShootEventにします。
- Tool内にLocalScrpitを入れ、以下のプログララムを貼り付けます。
local tool = script.Parent
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
tool.Activated:Connect(function()
-- プレイヤーがクリックした位置を取得
local targetPosition = mouse.Hit.p
-- サーバーに通知
tool.ShootEvent:FireServer(targetPosition)
end)
- 最後にToolの内にScript を入れ、以下のプログラムを貼り付ければ完成です。
local tool = script.Parent
local shootEvent = tool.ShootEvent
shootEvent.OnServerEvent:Connect(function(player, targetPosition)
-- 球体を生成
local ball = Instance.new("Part")
ball.Shape = Enum.PartType.Ball
ball.Size = Vector3.new(1, 1, 1)
ball.BrickColor = BrickColor.new("Bright red")
ball.Anchored = false
ball.CanCollide = false
ball.Parent = game.Workspace
-- 球体を生成する位置を計算
local handlePosition = tool.Handle.Position
local direction = (targetPosition - handlePosition).Unit -- ユニットベクトルを計算
local spawnPosition = handlePosition + direction * 2 -- Handleの少し前に配置
ball.Position = spawnPosition
-- 球体を発射
local bodyVelocity = Instance.new("BodyVelocity")
bodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
bodyVelocity.Velocity = direction * 50 -- 発射速度
bodyVelocity.Parent = ball
-- 衝突時の爆発処理
local touched = false
ball.Touched:Connect(function(hit)
if touched then return end
touched = true
-- 爆発を生成
local explosion = Instance.new("Explosion")
explosion.Position = ball.Position
explosion.BlastRadius = 5 -- 爆発の範囲
explosion.BlastPressure = 50000 -- 爆発の強さ
explosion.Parent = game.Workspace
-- 球体を削除
ball:Destroy()
end)
-- 一定時間後に削除
game:GetService("Debris"):AddItem(ball, 5)
end)
BodyVelocity を使わない方法
BodyVelocity は非推奨になってしまいました。代替えの LinearVelocity を使用した方法も掲載しておきます。
local tool = script.Parent
local shootEvent = tool.ShootEvent
local Debris = game:GetService("Debris")
shootEvent.OnServerEvent:Connect(function(player, targetPosition)
-- 球体を生成
local ball = Instance.new("Part")
ball.Shape = Enum.PartType.Ball
ball.Size = Vector3.new(1, 1, 1)
ball.BrickColor = BrickColor.new("Bright red")
ball.Anchored = false
ball.CanCollide = false
ball.Parent = workspace
-- 発射方向計算
local handlePosition = tool.Handle.Position
local direction = (targetPosition - handlePosition).Unit
local spawnPosition = handlePosition + direction * 2
ball.Position = spawnPosition
-- Attachment を作成
local attachment = Instance.new("Attachment")
attachment.Parent = ball
-- LinearVelocity を作成
local linearVelocity = Instance.new("LinearVelocity")
linearVelocity.Attachment0 = attachment
linearVelocity.MaxForce = math.huge
linearVelocity.VectorVelocity = direction * 50 -- 発射速度
linearVelocity.RelativeTo = Enum.ActuatorRelativeTo.World
linearVelocity.Parent = ball
-- 衝突時の爆発処理
local touched = false
ball.Touched:Connect(function(hit)
if touched then return end
touched = true
local explosion = Instance.new("Explosion")
explosion.Position = ball.Position
explosion.BlastRadius = 5
explosion.BlastPressure = 50000
explosion.Parent = workspace
ball:Destroy()
end)
-- 一定時間後に削除
Debris:AddItem(ball, 5)
end)
