Hi, so I’m currently trying to add a spin animation on the player’s head. For whatever reason, when I specify the amount of rotation to about 360, nothing happens. If it’s a smaller number, then it works, but it doesn’t go for the whole 360 degree rotation I’m looking for.
As it’s current state is actually 360 degrees, I mean the same orientation, I think it’s because tweenservice will not tween to equal values
though you can do
local t = tween_service:Create(headMotor, TweenInfo.new(.3), {C1 = headMotor.C1*CFrame.Angles(0, math.rad(359), 0)})
t:Play()
t.Completed:Wait()
tween_service:Create(headMotor, TweenInfo.new(.06), {C1 = headMotor.C1*CFrame.Angles(0, math.rad(1), 0)}):Play()
As stated before, TweenService will always take the shortest path possible to rotating. Something you could do is do a bunch of smaller tweens that actually take the correct path:
for i=1, 4 do
local nt = tween_service:Create(headMotor, TweenInfo.new(.0.075), {C1 = headMotor.C1*CFrame.Angles(0, math.rad(i * 90), 0)})
nt:Play()
nt.Completed:Wait()
end
It seems at though its taking the quickest route towards its goal. Meaning either clockwise or counter-clockwise depending on what’s faster. 180 degrees would always get you the maximum rotation.
So how to fix this: tween your rotation in steps of 180 degrees.
local function tweenRotation(rotation,duration)
local steps = math.floor(rotation / math.pi);
local excess = rotation - math.pi * steps;
local durationPerRadian = duration / rotation;
for i=1,steps do
local goal = {C1 = headMotor.C1 * CFrame.Angles(0,math.pi,0)}
local currTween = tweenService:Create(headMotor,TweenInfo.new(math.pi * durationPerRadian,Enum.EasingStyle.Linear) ,goal);
currTween:Play()
currTween.Completed:Wait()
end
-- Account for excess rotation
local goal = {C1 = headMotor.C1 * CFrame.Angles(0,excess,0)}
tweenService:Create(headMotor,TweenInfo.new(excess * durationPerRadian),goal):Play();
end
Edit: Made a bunch of typos, cause I was typing this really fast and on mobile. Another thing I forgot to mention was that this approach would only work if you using a linear tween of course.
Something I usually do when trying to tween rotations like this is I create a temporary NumberValue that acts as the degree and tween that instead. It should be something like this:
local angle = Instance.new("NumberValue")
RunService.Heartbeat:Connect(function()
headMotor.C1 = CFrame.new(headMotor.C1.Position) * CFrame.Angles(0, math.rad(angle.Value), 0)
end)
TweenService:Create(angle, tweenInfo, {
Value = 360;
}):Play();
[EDIT 1/7/2022]: Instead of using NumberValues and tweening them, it would probably work better to use a spring.