How can I preserve orientation of my model while moving it with TweenService?
I'm building a set of sliding doors that use TweenService when I press a button to trigger them.
There are two positions (opened and closed) that are represented using four invisible, massless and non-CanCollide bricks, which are identical to the real doors. (Door1Closed, Door1Opened, Door2Closed and Door2Opened)
Both doors are models, so I built a function ('tweenModel' in the code) to tween every part in the model independently rather than using SetPrimaryPartCFrame(), because it caused trouble with special welds elsewhere in my game.
It works, but it also tweens to an orientation of {0, 0, 0}.
Somebody mentioned that the problem stems from CFrame including orientation values as well as position, and me calculating CFrame from Vector3 (Position), gives it no values for orientation, causing it to resort to the default (which I assume is {0, 0, 0})
If this is the case, then how can I keep the orientation of my doors while using TweenService to translate them?
Here's a video of the doors:
https://imgur.com/qjWnSsO
Here's the script:
01 | local TweenService = game:GetService( "TweenService" ) |
02 | local Door 1 = script.Parent:WaitForChild( "Door1" ) |
03 | local Door 2 = script.Parent:WaitForChild( "Door2" ) |
04 | local Targets = script.Parent.Targets |
05 | local tweeningInformation = TweenInfo.new( |
07 | Enum.EasingStyle.Bounce, |
08 | Enum.EasingDirection.Out, |
15 | local OpenButton = script.Parent:WaitForChild( "OpenButton" ) |
16 | local CloseButton = script.Parent:WaitForChild( "CloseButton" ) |
18 | function tweenModel(model, tweeningInfo, vector) |
19 | local descendants = model:GetDescendants() |
20 | for i = 1 , #descendants do |
21 | if descendants [ i ] :IsA( "BasePart" ) then |
24 | local modVector = { CFrame = CFrame.new(descendants [ i ] .Position + vector) } |
26 | local temp = TweenService:Create(descendants [ i ] , tweeningInfo, modVector) |
34 | CloseButton.ClickDetector.MaxActivationDistance = 0 |
35 | CloseButton.BrickColor = BrickColor.new( "Neon orange" ) |
36 | print (Targets.Door 1 Closed.Position - Targets.Door 1 Opened.Position) |
37 | tweenModel(Door 1 , tweeningInformation, (Targets.Door 1 Closed.Position - Targets.Door 1 Opened.Position)) |
38 | tweenModel(Door 2 , tweeningInformation, (Targets.Door 2 Closed.Position - Targets.Door 2 Opened.Position)) |
40 | CloseButton.BrickColor = BrickColor.new( "Crimson" ) |
41 | OpenButton.BrickColor = BrickColor.new( "Electric blue" ) |
42 | OpenButton.ClickDetector.MaxActivationDistance = 32 |
45 | OpenButton.ClickDetector.MaxActivationDistance = 0 |
46 | OpenButton.BrickColor = BrickColor.new( "Neon orange" ) |
47 | tweenModel(Door 1 , tweeningInformation, (Targets.Door 1 Opened.Position - Targets.Door 1 Closed.Position)) |
48 | tweenModel(Door 2 , tweeningInformation, (Targets.Door 2 Opened.Position - Targets.Door 2 Closed.Position)) |
50 | OpenButton.BrickColor = BrickColor.new( "Crimson" ) |
51 | CloseButton.BrickColor = BrickColor.new( "Electric blue" ) |
52 | CloseButton.ClickDetector.MaxActivationDistance = 32 |
57 | OpenButton.ClickDetector.MouseClick:Connect(operate) |
58 | CloseButton.ClickDetector.MouseClick:Connect(operate) |