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

How do I make CFrame position random while also sticking within a certain distance on X/Z value?

Asked by 3 years ago

I am trying to make a tornado damage script and it works (kind of) but all parts hit by a tornado go to the same spot and I don't know how I could fix this even by looking at multiple CFrame tutorials. pls help pretty pls

video showing problem: https://streamable.com/cbfs9m

code:

local tornado = script.Parent
local tornadoOffset = CFrame.new(0, 0, tornado.Position.Z * 10)

tornado.Touched:Connect(function(hit)
    if hit.Name ~= "Baseplate" then
        hit.Anchored = false
        local damage = Instance.new("BodyPosition", hit)
        damage.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
        while wait() do
            damage.Position = tornado.CFrame:ToWorldSpace(tornadoOffset).p
        end
    end
end)

2 answers

Log in to vote
0
Answered by
Speedmask 661 Moderation Voter
3 years ago
Edited 3 years ago
local RAD_MAX = 10
local angle = math.rad(math.random(360))
local radius = RAD_MAX * math.random(-1000, 1000) / 1000
local randOffset = Vector3.new(math.cos(angle), 0, math.sin(angle)) * radius
local tornadoOffset = CFrame.new(tornado.Position + randOffset)

the offset is a random position in the circle around the tornado with a radius of RAD_MAX. you can also mess around with the height and rotation of CFrames to make it “truly” random. if you don’t understand the math, I think it is important that you do, so I suggest you look up how to find a point on a circle.

0
Omg even better than my solution lol thank you so much! I kind of understand the math but I shall look into it more, thank you again MattWasTaken96 18 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Instead of multiplying the Z by 10, I would use math.random(10), which chooses a random number between 1 and 10 and multiplies it with the Z.

0
thank you for the response, I shall try later and let you know MattWasTaken96 18 — 3y
0
I have tried it but It hasn't really helped, I'm not sure what I'm doing wrong MattWasTaken96 18 — 3y
0
Okay so, I mean it worked but then the parts just have a seziure in the sky. I need them to rotate around the tornado while going to random positions smoothly, I might try and use something else instead of body position MattWasTaken96 18 — 3y

Answer this question