Tweening: Part tilts for some reason

I’m making an idle turret of sorts, anyways, it’s cosmetic and supposed to be simple. My problem lies with tweening and probably the calculation or the application. It’s supposed to rotate, and elevate, then rotate, etc. Which works, but as soon as it rotates after it elevates to a degree higher/lower than 0, the ‘elevationPart’, the higher one, tilts to its sides, adjusting it’s Z axis, causing the barrels to be in an awkward position.

I tried manipulating the orientation directly, using Vector3, though…
If I dont CFrames, it wont tween the welded parts for some reason, unless I’m missing something?

As a side note: I made the elevationPart, rotate with the rotationalPart, so it remains consistent. If there’s a better way to do this better, please feel free to share.

As you can see it’s not behaving like a proper turret:

Relevant Functions:

local function rotateTo(targetRotation)
	local targetRotationalCFrame = CFrame.Angles(math.rad(targetRotation), 0, 0)
	local targetElevationCFrame = CFrame.Angles(0, math.rad(-targetRotation), 0)

	local currentRotationalCFrame = rotationalPart.CFrame
	local currentElevationCFrame = elevationPart.CFrame

	local newRotationalCFrame = currentRotationalCFrame * targetRotationalCFrame
	local newElevationCFrame = currentElevationCFrame * targetElevationCFrame

	local tweenRotational = tweenService:Create(
		rotationalPart,
		TweenInfo.new(
			SPEED,
			EASING_STYLE,
			Enum.EasingDirection.InOut
		),
		{
			CFrame = newRotationalCFrame
		}
	)

	local tweenElevation = tweenService:Create(
		elevationPart,
		TweenInfo.new(
			SPEED,
			EASING_STYLE,
			Enum.EasingDirection.InOut
		),
		{
			CFrame = newElevationCFrame
		}
	)

	print('rotating')
	tweenRotational:Play()
	tweenElevation:Play()

	isPlaying = true
	return tweenRotational
end

local function elevateTo(targetElevation)
	local tweenAlpha = tweenService:Create(
		elevationPart,
		TweenInfo.new(
			SPEED,
			EASING_STYLE,
			Enum.EasingDirection.InOut
		),
		{
			CFrame = elevationPart.CFrame * CFrame.Angles(math.rad(targetElevation), 0, 0)
		}
	)

	print('elevating')
	tweenAlpha:Play()
	isPlaying = true
	return tweenAlpha
end

They play after each other completes, eg. Rotate → Elevate → Rotate → etc.

If there are other methods that may be better than what I’m trying to do here, please feel free to share.

P.S. I’ll be gone for a while, so I might not reply sooner.

2 Likes

Can you print the CFrame values used in the tweens? Then check the rotation of that CFrame, if everything seems fine, then the problem is probably in the tweenInfo, somehow.

And can you show us the results, and which print that was?

2 Likes

The problem is probably here at this line:

local newElevationCFrame = currentElevationCFrame * targetElevationCFrame

and it should be replaced with

local newElevationCFrame = CFrame.Angles(0, math.rad(-targetRotation), 0) * (elevationPart.CFrame - elevationPart.CFrame.Position) + elevationPart.CFrame.Position

If this didn’t help then I’m sorry, but if it did then here’s the explanation: I don't understand how to rotate stuff in world space - #2 by BanTech

Hopefully this did atleast give you a hint!

1 Like

Greetings I’m back. I thank you, this seems to be the solution.

1 Like

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