是否可以使焊接在人形根部上的部件随之移动? 它充当碰撞箱,但我遇到了一个问题,如果我在攻击时向前移动,就会伤害玩家。 为此我有两个脚本 本地脚本:
`local cas = game:GetService("ContextActionService")
local rs = game:GetService("ReplicatedStorage")
local events = rs:WaitForChild("Events")
local hitbox = events:WaitForChild("Hitbox")
local plr = game.Players.LocalPlayer
local character = plr.Character
if not character then
plr:WaitForChild("Character")
end
local humanoid = character.Humanoid
local animator = humanoid:WaitForChild("Animator")
local idle = animator:LoadAnimation(script:WaitForChild("idle"))
local jab = animator:LoadAnimation(script:WaitForChild("jab"))
local rightcross = animator:LoadAnimation(script:WaitForChild("rightstraight"))
local lefthook = animator:LoadAnimation(script:WaitForChild("lefthook"))
local currentPunch = 0
local debounce = false
local function punch()
if debounce then return end
debounce = true
if currentPunch == 0 then
jab:Play()
hitbox:FireServer(Vector3.new(3, 5, 2), Vector3.new(4.5, 1), 10, 0.1)
task.wait(0.5)
jab:Stop()
debounce = false
elseif currentPunch == 1 then
jab:Play()
hitbox:FireServer(Vector3.new(3, 5, 2), Vector3.new(4.5, 1), 10, 0.1)
task.wait(0.5)
jab:Stop()
debounce = false
elseif currentPunch == 2 then
rightcross:Play()
hitbox:FireServer(Vector3.new(3, 5, 2), Vector3.new(4.65, 1), 10, 0.1)
task.wait(0.5)
rightcross:Stop()
debounce = false
elseif currentPunch == 3 then
lefthook:Play()
hitbox:FireServer(Vector3.new(3, 5, 2), Vector3.new(4, 1), 10, 0.2)
task.wait(0.5)
lefthook:Stop()
debounce = false
end
if currentPunch == 3 then
currentPunch = 0
debounce = true
wait(1.5)
debounce = false
else
currentPunch += 1
end
end
cas:BindAction("Punch", punch, true, Enum.UserInputType.MouseButton1)
服务器端:
local rs = game:GetService("ReplicatedStorage")
local events = rs:WaitForChild("Events")
local hitboxEvent = events:WaitForChild("Hitbox")
function newHitbox(character, size, offset, damage, linger)
local hrp = character:FindFirstChild("HumanoidRootPart")
if hrp == nil then return end
local weld = Instance.new("WeldConstraint")
local hitbox = Instance.new("Part")
weld.Part0 = hrp
weld.Part1 = hitbox
hitbox.CanCollide = false
hitbox.CanQuery = false
hitbox.Massless = true
hitbox.Anchored = true
hitbox.Size = size
hitbox.CFrame = hrp.CFrame + hrp.CFrame.LookVector * offset.X + Vector3.new(0, offset.Y)
hitbox.Parent = character
hitbox.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") == nil then return end
for _, v in pairs(hitbox:GetChildren()) do
if v:IsA("ObjectValue") then
if v.Value == hit.Parent then return end
end
end
local hitCounter = Instance.new("ObjectValue", hitbox)
hitCounter.Value = hit.Parent
hit.Parent.Humanoid:TakeDamage(damage)
end)
task.wait(linger)
hitbox:Destroy()
end
hitboxEvent.OnServerEvent:Connect(function(plr, size, offset, damage, linger)
newHitbox(plr.Character, size, offset, damage, linger)
end)
LocalScript
位于 StarterGUI
服务器脚本位于 ServerScriptService
我可以让玩家在点击时减慢速度,但我只是想知道答案,这样我就可以成长为一名程序员。
可以制作一个焊接到玩家的 HumanoidRootPart 上的部件。您可以通过将焊接对象实例化到玩家的躯干来完成此操作。这是一个例子:
game.Players.PlayerAdded:connect(function(player)
player.CharacterAdded:connect(function(char)
local torso = char:WaitForChild("HumanoidRootPart") --You can change this to another body part
local newPart = Instance.new("Part", char)
newPart.Massless = true --You will likely want to set at least these properties
newPart.CanCollide = false
newPart.Anchored = false
local weld = Instance.new("Weld", torso) --Could also use a Motor6D, or likewise
weld.C0 = CFrame.new() --Both C0 and C1 are, by default, set to a blank CFrame
weld.C1 = CFrame.new()
weld.Part0 = torso
weld.Part1 = newPart
end)
完)
来源:这里