בובת אימונים
שיעור 5 מתוך 25 בקורס משחק Roblox: זירות קרב של Coddy.
בובת האימון זקוקה לשני דברים כדי להיות בובת אימון: מד בריאות, ושיופיע מחדש כשמנצחים אותה.
ה־HealthBar שלה הוא BillboardGui עם Fill ירוק בתוך Back כהה. UDim2.fromScale(health / MaxHealth, 1) קובע את רוחב המילוי לפי כמות הבריאות שנותרה.
כשה־Humanoid שלה מת (Died), זכרו היכן הוא עמד (GetPivot), הסירו אותו, המתינו Settings.DummyRespawn שניות והציבו במקומו עותק חדש מ־ServerStorage. צריך להגדיר גם את הבובה החדשה באותו אופן, לכן ההגדרה היא פונקציה שקוראת לעצמה עבור הבובה החדשה:
local function setupDummy(dummy)
-- סרגל בריאות, וכאשר Died מתרחש: ... setupDummy(fresh)
end
setupDummy(workspace.Dummy)אתגר
קלהוסף לבובת האימון מד בריאות והחזר אותה כשהיא מובסת.
- כתוב
setupDummy(dummy): בעתHealthChanged, שנה את הגודל שלdummy.HealthBar.Back.Fillבהתאם לבריאות שנותרה. - בעת
Died: שמור אתdummy:GetPivot(), המתן 0.5 שניות והשמד את בובת האימון. - לאחר מכן המתן עד
Settings.DummyRespawn, שכפל אתServerStorage.Dummyאל נקודת הציר הזו ב-workspace, וקרא להפעיל עליה אתsetupDummy. - קרא ל-
setupDummy(workspace.Dummy).
לחץ על "שחק" והבס את בובת האימון: המד שלה מתרוקן, והיא חוזרת. לאחר מכן לחץ על "בדיקה".
נסו בעצמכם
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")
-- הנפת חרב: תחילה האנימציה, ואז כל מי שנמצא בתוך התיבה שלפניך נפגע
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()
-- התיבה שלפניך, לעולם לא הגוף שלך
local params = OverlapParams.new()
params.FilterDescendantsInstances = { character }
local box = root.CFrame * CFrame.new(0, 0, -3)
local hit = {} -- כל humanoid פעם אחת בכל הנפת חרב
for _, part in workspace:GetPartBoundsInBox(box, Settings.HitboxSize, params) do
local humanoid = part.Parent:FindFirstChildOfClass("Humanoid")
if humanoid and not hit[humanoid] then
hit[humanoid] = true
humanoid:TakeDamage(Settings.SwordDamage)
print(humanoid.Parent.Name .. " is at " .. humanoid.Health)
end
end
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)
-- שיעור 5: בובת האימון תופיע כאן
כל השיעורים ביחידה משחק Roblox: זירות קרב
תרגלו בעצמכם: Roblox אונליין