What exactly does this code do?
I think gets the position right? (idk)
but that I’m trying to say here is I want to understand the code so I may use it later on when i might need it.
How does this work? Your subtracting CFrame.Position from CFrame? uhhhh… huh?
Is there maybe a mathematical problem someone can explain plz plz
local currentrotation = tor.CFrame - tor.CFrame.Position
Here is the full loop.
for i = 0, dist, 1 do
local currentrotation = tor.CFrame - tor.CFrame.Position
tor.CFrame = CFrame.new(tor.Position + unit * dist / 25) * currentrotation
wait()
end
It’s hard to know what tor is and what other variables in the code mean with these snippets, but it just seems to be the code for rotating the models in the Viewport tabs.
Nevermind that, I thought it was the Toolbox’s plugin code. Yes, the first thing simply removes the position from the CFrame (interesting approach) and the second thing just acts as a weird Tween replacement?
a unit vector is a vector with its magnitude equal to 1, its basically a direction without distance, tor.Position + unit will make that direction relative to tors position
I’ll just let yall look at the full script here cause it seems that its a little outdated and might not be a reliable way to make a tornado (tor)
local tor = script.Parent
local mesh = tor.Mesh
local rootpos = tor.Position
local restingTime = math.random(1, 3)
local maxMovingDistance = 10
local maxAllowedRange = 50*20
local spin = coroutine.resume(coroutine.create(function()
while wait() do
tor.CFrame = tor.CFrame * CFrame.fromEulerAnglesXYZ(0, math.rad(-2), 0)
end
end))
------------------------------------------------
local move = coroutine.resume(coroutine.create(function()
while wait(restingTime) do
local pickedpos = tor.Position + Vector3.new(math.random(-maxMovingDistance, maxMovingDistance), 0, math.random(-maxMovingDistance, maxMovingDistance))
local dist = (pickedpos - tor.Position).magnitude
local unit = (pickedpos - tor.Position).unit
print(dist)
print(unit)
-- move the tornado in a random position
for i = 0, dist, 1 do
local currentrotation = tor.CFrame - tor.CFrame.Position
tor.CFrame = CFrame.new(tor.Position + unit * dist / 25) * currentrotation
wait()
end
--check if the tornado goes too far, if so it will go back to it's root position
if (pickedpos - rootpos).magnitude >= maxAllowedRange then
local ldist = (pickedpos - rootpos).magnitude
local lunit = (pickedpos - rootpos).unit
print("Tornado went too far, going back (distance: " ..ldist.. " studs away)")
for i = 0, ldist, 1 do
local currentrotation = tor.CFrame - tor.CFrame.Position
tor.CFrame = CFrame.new(tor.Position - lunit * ldist / (ldist*0.75)) * currentrotation
wait()
end
end
end
end))
Yes, I love to figure these out… This seems to be part of a Tornado set up. I put what I thought belonged together from tool box parts and it kind of works… Enough to call it a Tornado set up. Tornato.rbxm (5.7 KB)
I’d say this is a Tornado that is meant to move in a random direction then move back and repeat.
I made it a large one but it could have even been smaller ones too.
I know what the code does in general but its just I want to understand so I can make things on my own. I usually get confused by things like CFrame + unit or some other things like that cause i don’t know how they work
I highly recommend TweenService. It is much smoother and more reliable. You don’t have to worry about it going to far and moving back to the middle, as it just smoothly moves to the position given, even if its random.