אזור פגיעה
שיעור 3 מתוך 25 בקורס משחק Roblox: זירות קרב של Coddy.
תנופה צריכה לפגוע במשהו. השאלה "מה נמצא לפניי?" היא שאילתה מרחבית: workspace:GetPartBoundsInBox מקבלת תיבה (CFrame עבור המרכז שלה ו-Vector3 עבור הגודל שלה) ומחזירה כל חלק שחופף לה.
local box = root.CFrame * CFrame.new(0, 0, -3) -- 3 סטאדים מלפנים
local parts = workspace:GetPartBoundsInBox(box, Settings.HitboxSize, params)המודל של חלק מכיל את ה-Humanoid, ולכן part.Parent:FindFirstChildOfClass("Humanoid") מציין אם הוא שייך למישהו שאפשר לפגוע בו.
גם הזרועות שלך נמצאות בתוך התיבה הזאת. OverlapParams מסננים את השאילתה: כאשר הדמות שלך נמצאת ב-FilterDescendantsInstances, התיבה לעולם לא תמצא את הגוף שלך.
אתגר
קלגרום לכך שהנפה תפגע בכל מי שעומד מולך.
- בסוף
swing, צורOverlapParamsעםFilterDescendantsInstances = { character }. - עבור כל חלק שמוחזר מ־
GetPartBoundsInBoxשלroot.CFrame * CFrame.new(0, 0, -3), בגודלSettings.HitboxSize: אם להורה שלו יש Humanoid, הפעלTakeDamage(Settings.SwordDamage). - לאחר מכן, הפעל
print(humanoid.Parent.Name .. " is at " .. humanoid.Health).
לחץ על "שחק" ופגע בבובת האימון: ערך הבריאות שלה יורד בחלון הפלט. לאחר מכן לחץ על "בדיקה".
נסו בעצמכם
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)
local lastSwing = {} -- player -> מתי הניף את החרב בפעם האחרונה
-- אנימציות החרב של Roblox עצמה
local slash = Instance.new("Animation")
slash.AnimationId = "rbxassetid://522635514"
print("Swords deal " .. Settings.SwordDamage .. " damage")
-- הנפת חרב: האנימציה, לכל היותר פעם אחת בכל פרק זמן של SwingCooldown
local function swing(player)
local character = player.Character
local root = character and character:FindFirstChild("HumanoidRootPart")
if not root or character.Humanoid.Health <= 0 then
return
end
local now = time()
if lastSwing[player] and now - lastSwing[player] < Settings.SwingCooldown then
return
end
lastSwing[player] = now
character.Humanoid.Animator:LoadAnimation(slash):Play()
-- שיעור 3: כאן יופיע ה-Hitbox
end
-- כל שחקן מקבל חרב בכל פעם שהוא נוצר מחדש
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function()
local sword = ServerStorage.Sword:Clone()
sword.Parent = player.Backpack
sword.Activated:Connect(function()
swing(player)
end)
end)
end)
כל השיעורים ביחידה משחק Roblox: זירות קרב
תרגלו בעצמכם: Roblox אונליין