Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer
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.

1 comment

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

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.

3 comments

0

thank you for the response, I shall try later and let you know

0

I have tried it but It hasn't really helped, I'm not sure what I'm doing wrong

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

This discussion is preserved as it was. New answers, comments, and votes are closed.