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

How do I make a part peacefully but smoothly rotate in different directions?

I have this script I made, where, a part constantly rotates in different directions. The problem is, it completely SPAZZES out. I want it to still move randomly, but without violent and ugly rotations. Here's my script:

local part = script.Parent
local c = part.CFrame

while wait() do
	part.CFrame = part.CFrame * CFrame.Angles(math.random(-1,1),math.random(-1,1),math.random(-1,1))
end

All help is appreciated!

1 comment

0

Try just math.random

1 answer

The problem is that you are randomly rotating the part by a very large amount (1 radian is almost 57.3 degrees). Therefore you could generate a rotation very far from the current one, making it jump really quickly.

But even if you make this value smaller it will give a shaking and not very smooth effect. The best thing to do would be to interpolate between the old and new rotations. Luckily, this is quite easy using delta time and the CFrame lerp function:

local part = script.Parent
local duration = 1 -- How long the interpolation duration is

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

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

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