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

How to I rotate a part smoothly?

Hello,

I'm trying to make a spinner which the player stands on to move around the map. I have tried to use BodyGyro and CFrame to do this.

I find that CFrame studders from degree to degree, which doesn't look very aesthetic. This was my attempt at using CFrame to move the part:

local brick = script.Parent
local a = 0
while true do
	wait()
	local a = a + 0.04
	brick.CFrame  = brick.CFrame * CFrame.Angles(0, a ,0)
end

Again, this did work, but it wasn't very smooth.

I couldn't get the BodyGyro attempt to work either. Firstly, because I had to unanchor the block, causing the block to fall out of the map. Secondly, because it just didn't rotate :(

This was my attempt on using BodyGyro to rotate the part:

local brick = script.Parent
local body = script.Parent:FindFirstChild("BodyGyro")
local a = body.CFrame.Y
while true do
	wait()
	local a = a + 1
	body.CFrame = CFrame.Angles(0,a,0)
end

And again, this script didn't work at all :(

I would be grateful if you could help me create this smooth rotating block. Thanks.

1 comment

0

To make a part rotate smoothly, you should use TweenService and do the tweening on the client.

1 answer

✓ Accepted answer

you can use TweenService to tween the part smoothly.

local ts = game:GetService("TweenService")
local part = script.Parent -- lets say this was the part

local info = TweenInfo.new(5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out,-1)
local properties = {CFrame = script.Parent.CFrame * CFrame.Angles(0,math.rad(180),0)}

local tween = ts:Create(script.Parent,info,properties)

tween:Play() -- plays the tween

--[[
	tween:Cancel() -- stops the tween
	tween.Completed -- when the tween finishes
--]]

TweenInfo.new() actually takes 6 parameters, though you dont need to use all of them. the first parameter is how long should the tween go to the objective, for this it would take 5 seconds to rotate 180 degrees. Enum.EasingStyle.Linear is just a easing style, you can choose other easing styles too. the rest you can find out here TweenInfo.new().

TweenService

######you can also tween other stuff; Color, CFrame or Position, and UDim2 values etc.

5 comments

0

TweenService is probably your best option in this situation.

0

Remember that pi is 180 degrees in radians, so you can do math.pi instead of math.rad(180), and for a 90 degree turn you can do math.pi/2, and so on.

0

Is this code in a server script, and is the rotating part CanCollide=true? If you answer yes to either of these, TweenService is most assuredly NOT what you want to use.

0

if you do print (math.pi) will it print 3.141592653589793238462643383279502884197169399?

0

@HappyTimIsHim the only way to get that number to show up like that is to float the number to remove the e-notation

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