So i'm trying to make a cube that spins in random directions smoothly, but when the script chooses another random direction for the cube to go, it just instantly goes into that direction

i'm trying to make it slowly stop and move into that direction

any solutions?

here is my (normal) script inside of the cube:

local part = script.Parent
local part2 = script.Parent.Parent.Outer
local duration = 5 -- How long the interpolation duration is

local function rand() -- Random number between -180 and 180 degrees in radians
    return math.random()*30*math.pi - math.pi
end

while true do
    local firstTick = tick()
	    local start = part.CFrame
		local start2 = part2.CFrame
	    local goal = start * CFrame.Angles(rand(), rand(), rand())
		local goa2 = start * CFrame.Angles(rand(), rand(), rand())
    repeat
        local elapsed = tick() - firstTick
        local alpha = math.min(1, elapsed/duration)
		        part.CFrame = start:lerp(goal, alpha)
				part2.CFrame = start2:lerp(goal, alpha)
        wait()
    until alpha == 1
end


1 comment

0

You could use the tweenservice, Lerp, or a for loop.