How to Tween Angled Model

Hi everyone,

As you can infer from the title, I’m struggling to tween a model - a piston - that is set on an angle (-15). I need it to go up and down from its original position. However, my current script makes the object itself move across the workspace (another variation of the script makes it rotate oddly). See the script below for critique:

local TweenService = game:GetService("TweenService")

local PistonModel = script.Parent
local PistonPrimaryPart = PistonModel.Main

local TweenInfo = TweenInfo.new(
	0.33,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.In,
	-1,
	true
)

TweenService:Create(PistonPrimaryPart, TweenInfo, {CFrame = CFrame.new(234.987, 19.347, 0)}):Play()

To delineate, I would like the part to move from

234.87, 19.782, -156.7

to

234.987, 19.347, -156.7

Thanks! :happy1:

1 Like

Bumping this post; I’ve tried manipulating the PrimaryPart’s CFrame angle using math.rad to no avail…

Do you want to tween a piston model to move up and down in a straight line? If so, here:

local TweenService = game:GetService("TweenService")

local PistonModel = script.Parent
local PistonPrimaryPart = PistonModel.Main

local initialPosition = PistonPrimaryPart.Position
local targetPosition = initialPosition + Vector3.new(0, -0.435, 0)  -- Adjust the Y value as needed

local TweenInfo = TweenInfo.new(
    0.33,
    Enum.EasingStyle.Linear,
    Enum.EasingDirection.InOut,
    -1,
    true
)

local tween = TweenService:Create(PistonPrimaryPart, TweenInfo, {Position = targetPosition})
tween:Play()

-- To make it move up and down continuously, you can set up a completed event.
tween.Completed:Connect(function()
    PistonPrimaryPart.Position = initialPosition  -- Reset the position
    tween:Play()  -- Restart the tween
end)
1 Like

Thanks! :happy1:

I’ve changed the script, as you recommended, and it works sufficiently. However, it does seem to wobble a bit on the x-axis. I’ve altered the x value (to mitigate this as much as possible), which you can see in the script below. Is it possible to make the tween smoother?

local TweenService = game:GetService("TweenService")

local PistonModel = script.Parent
local PistonPrimaryPart = PistonModel.Main

local StartingPosition = PistonPrimaryPart.Position
local EndPosition = StartingPosition + Vector3.new(0.1, -0.435, 0)

local TweenInfo = TweenInfo.new(
	0.33,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.In,
	-1,
	true
)

local tween = TweenService:Create(PistonPrimaryPart, TweenInfo, {Position = EndPosition})
tween:Play()

tween.Completed:Connect(function()
	PistonPrimaryPart.Position = StartingPosition
	tween:Play()
end)

Sure! Right now I’m starting my commision but I will help you later in the day. I do see it tilting a little bit, and I’ll try to get back to you as soon as possible.

1 Like
local TweenService = game:GetService("TweenService")

local PistonModel = script.Parent
local PistonPrimaryPart = PistonModel.Main

local StartingPosition = PistonPrimaryPart.Position
local EndPosition = StartingPosition + Vector3.new(0.1, -0.435, 0)

local TweenInfo = TweenInfo.new(
    0.33,
    Enum.EasingStyle.Linear,
    Enum.EasingDirection.In,
    -1,
    true
)

local tween = TweenService:Create(PistonPrimaryPart, TweenInfo, {Position = EndPosition})

tween:Play()

tween.Completed:Connect(function()
    PistonPrimaryPart.Position = StartingPosition
    tween:Play()
end)

This might be smoother. Change anything you don’t like.

Does the other parts move? Like do they all move at the same time?

^ If so, can I see?

Or is it just that one?

I’ll apply the script to the other pistons, considering pistons work independently and out-of-sequence with each other.

I’ve noticed no change with the script you provided me. That’s ok. I’ve adjusted the tween speed to reflect how they move in real life, so the swaying is barely noticeable.

Thanks for your help!

No problem! Feel free to reach out to me if you have any questions. Good luck in your game!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.