OliePapis
(Olie_progamer)
1
Hello guys,
I’m trying to tween CFrame values, I’m going to be use it to make the wings of a butterfly flap.
Here’s my the script:
local tweenService = game:GetService("TweenService")
local runService = game:GetService("RunService")
local butterfly = script.Parent.Parent
local leftWing = butterfly.LeftWing
local rightWing = butterfly.RightWing
local wingHinge = butterfly.WingHinge
local hingePos = wingHinge.Position
local angleUp = 55
local angleDown = 0
local flapDuration = 30
local lWingPos = Instance.new("CFrameValue")
lWingPos = leftWing.CFrame
local lWingUp = Instance.new("CFrameValue")
lWingUp = CFrame.new(wingHinge.Position) * CFrame.Angles(0, 0, math.rad(angleUp)) * CFrame.new((leftWing.Size.X / 2) + (wingHinge.Size.Y /2), (leftWing.Size.Y / 2) + (wingHinge.Size.Y /2), 0)
local tweenTime = TweenInfo.new(flapDuration)
local leftWingTweenUp = tweenService:Create(lWingPos, tweenTime, lWingUp)
leftWingTweenUp:Play()
local conn = runService.Heartbeat:Connect(function()
leftWing.CFrame = lWingPos.Value
end)
When I run it, I’m getting the following error:
Unable to cast value to Object
Which is point to:
local leftWingTweenUp = tweenService:Create(lWingPos, tweenTime, lWingUp)
Would appreciate some here, I thought both lWingPos and lWingUp would be same type.
I’m a little bit lost here.
Thanks in advance.
2 Likes
The 1st argument to TS:Create is the part, not position.
1 Like
OliePapis
(Olie_progamer)
3
Hi @infiniteRaymond, thanks for your reply.
Accordingly to the documentation the very first argument is an instance, in which case I’m compliant.
In fact, if I remove the line:
lWingPos = leftWing.CFrame
I get no errors, although the animation doesn’t work really well, because I would be creating a CFrameValue instance with zeroed values.
1 Like
Ohh, alright.
Try this then:
local leftWingTweenUp = tweenService:Create(lWingPos, tweenTime, {Value = lWingUp.Value})
1 Like
Why not just rig the Butterfly and animate it?
1 Like
Because he wants to do tweening. Besides, not everybody is a great animator :v
1 Like
No, I totally understand. Just didn’t know if he was able to animate better and requires less code. Was just giving my opinion. Sorry.
2 Likes
OliePapis
(Olie_progamer)
8
Thanks everybody for trying to help.
I just cracked it…
It’s simpler and more efficient, here’s the code in case somebody may need in the future:
local tweenService = game:GetService("TweenService")
local runService = game:GetService("RunService")
local butterfly = script.Parent.Parent
local leftWing = butterfly.LeftWing
local rightWing = butterfly.RightWing
local wingHinge = butterfly.WingHinge
local hingePos = wingHinge.Position
local angleUp = 55
local angleDown = 0
local flapDuration = .2
local XOffset = (leftWing.Size.X / 2) - (wingHinge.Size.X / 2)
local YOffset = (leftWing.Size.Y / 2)
local ZOffset = (wingHinge.Size.Z / 2)- (wingHinge.Size.Z / 2)
local wingAngle = Instance.new('NumberValue')
wingAngle.Value = 0
local tweenTime = TweenInfo.new(flapDuration, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut,0,false,0)
local flapUpTween = tweenService:Create(wingAngle, tweenTime, { Value = angleUp })
local flapDownTween = tweenService:Create(wingAngle, tweenTime, { Value = angleDown })
flapUpTween.Completed:Connect(function(playbackState)
flapDownTween:Play()
end)
flapDownTween.Completed:Connect(function(playbackState)
flapUpTween:Play()
end)
flapUpTween:Play()
local conn = runService.Heartbeat:Connect(function()
leftWing.CFrame = CFrame.new(wingHinge.Position) * CFrame.Angles(0, 0, math.rad(wingAngle.Value)) * CFrame.new(XOffset, YOffset,ZOffset)
rightWing.CFrame = CFrame.new(wingHinge.Position) * CFrame.Angles(0, 0, math.rad(180 - wingAngle.Value)) * CFrame.new(XOffset, -YOffset,ZOffset)
end)
2 Likes