Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I add rotation to this random item spawning system?

Asked by
drew1017 330 Moderation Voter
9 years ago
chances = {
    ['FuelCrate'] = 85,
    ['HealthCrate'] = 15,
}

script.Parent.Spawn.OnServerEvent:connect(function()
local sum = 0
for thing, chance in pairs(chances) do
    sum = sum + chance
end


local index = math.random() * sum

local choice
for thing, chance in pairs(chances) do
    index = index - chance
    if index <= 0 then
        choice = thing
        break
    end
end

    local spawnedLoot = game.ReplicatedStorage.Loot[choice]:Clone()
spawnedLoot.Parent = workspace
spawnedLoot:MoveTo(script.Parent.Position)
--Code for rotating goes here
end)

Title; The reason is that i dont know exactly how to do this kind of rotation (ive only use CFrame.Angles once), and im afraid ill break something if I do it myself, so i'd like some help from people more experienced on CFrame.

The reason I need to rotate it is that it always spawns in the same direction, so it might cause certain loot types to appear to be empty then they're full, blend in with the structures etc.

1 answer

Log in to vote
1
Answered by
Redbullusa 1580 Moderation Voter
9 years ago

I'd suggest sticking to CFrame rather than Vector3 methods, so on line 26, replace it with this.

spawnedLoot:SetPrimaryPartCFrame(script.Parent.CFrame)

Onto rotation, CFrame.Angles() or CFrame.fromEulerAnglesXYZ() will assume its parameters is in radians, not degrees. You could use math.rad, because it's shorter than converting it, or math.pi if you're a nerd. ;)

-- This will give the model a 60-degree rotation window

local Angle = math.random(-30, 30)

spawnedLoot:SetPrimaryPartCFrame(script.Parent.CFrame * CFrame.Angles(0, math.rad(Angle), 0))

--[[ If you want to give the model a 60-degree rotation window, but in increments of ten...

local Angle = math.random(-3, 3)

spawnedLoot:SetPrimaryPartCFrame(script.Parent.CFrame * CFrame.Angles(0, math.rad(Angle * 10), 0))

]]

Now, let's see what's going on in the argument of the aforementioned method.

script.Parent.CFrame * CFrame.Angles(0, math.rad(Angle), 0)

On the first half, or script.Parent.CFrame, the model's position and rotation direction will be the same as script.Parent. Essentially, the model is at script.Parent and is also facing the way as script.Parent.

On the second half, or * CFrame.Angles(0, math.rad(Angle), 0), you're basically "adding"* rotation between -30° or 30°, or it will rotate to the left up to 30° or it will rotate to the right 30°.


*NOTE: The CFrame's consituents are actually being multiplied, hence the multiplication symbol. I only mentioned "adding", because the end result is basically adding rotation; it's for the sake of understanding the topic.

Ad

Answer this question