Align Orientation Flinging Towers

For these past few days, I have been having problems with the Align Orientation flinging my towers whenever they try to look at the enemy behind them. I want to achieve an align that makes the tower move to the target fast but not fling itself.

Module Script:

local delayTime = 0.4

function align.LookAt(Figure, Target)
	local dist = (Target.HumanoidRootPart.Position - Figure.HumanoidRootPart.Position).Magnitude
	local dir = (Target.HumanoidRootPart.Position - Figure.HumanoidRootPart.Position).Unit
	local vecA = Vector2.new(dir.X, dir.Z)
	local vecB = Vector2.new(Figure.HumanoidRootPart.CFrame.LookVector.X, Figure.HumanoidRootPart.CFrame.LookVector.Z)
	local dotProd = vecA:Dot(vecB)
	local crossProd = vecA:Cross(vecB)
	local angle = math.atan2(crossProd, dotProd)

	local ht = Target.HumanoidRootPart.Position.Y - Figure.HumanoidRootPart.Position.Y
	local UDA = math.atan(ht/dist)
	TweenService:Create(Figure.Torso["Left Shoulder"], TweenInfo.new(delayTime), {C0 = CFrame.new(Figure.Torso["Left Shoulder"].C0.Position) * CFrame.Angles(UDA, 0, 0) * CFrame.Angles(0, math.rad(-90), 0)}):Play()
	TweenService:Create(Figure.Torso.Neck, TweenInfo.new(delayTime), {C0 = CFrame.new(Figure.Torso.Neck.C0.Position) * CFrame.Angles(UDA, angle, 0) * CFrame.Angles(math.rad(-90), 0, math.rad(-180))}):Play()
	TweenService:Create(Figure.Torso["Right Shoulder"], TweenInfo.new(delayTime), {C0 = CFrame.new(Figure.Torso["Right Shoulder"].C0.Position) * CFrame.Angles(UDA, 0, 0) * CFrame.Angles(0, math.rad(90), 0)}):Play()
	if not Figure.HumanoidRootPart:FindFirstChild("Rotate") then
		local AlignOrientation = Instance.new("AlignOrientation")
		AlignOrientation.Name = "Rotate"
		AlignOrientation.Mode = Enum.OrientationAlignmentMode.OneAttachment
		AlignOrientation.AlignType = Enum.AlignType.PrimaryAxisLookAt
		AlignOrientation.Attachment0 = Figure.HumanoidRootPart.Face
		AlignOrientation.RigidityEnabled = true
		AlignOrientation.CFrame = CFrame.new(Target.HumanoidRootPart.CFrame.Position.X, Figure.HumanoidRootPart.CFrame.Position.Y, Target.HumanoidRootPart.CFrame.Position.Z)
		AlignOrientation.Parent = Figure.HumanoidRootPart
	else
		Figure.HumanoidRootPart.Rotate.CFrame = CFrame.new(Target.HumanoidRootPart.CFrame.Position.X, Figure.HumanoidRootPart.CFrame.Position.Y, Target.HumanoidRootPart.CFrame.Position.Z)
	end
end

And of course, there’s a script that updates the aligning with a while loop.

What I have tried so far is making the responsiveness a little lower but it always doesn’t reach the goal quickly. I tried asking for help on a discord server. But that pretty much didn’t help me.

Here’s a video and a picture:


Any help would be helpful. :slightly_smiling_face:

still need help please. No one has helped me so far.

I think it’s because of both RigidityEnabled and certain welded parts (such as the gun) not being massless or CanCollide off. A sudden, abrupt turn is probably what causes the physics to freak out.

Though I would reccomend switching to a Motor6D turning system, since it’s a bit more reliable.

Not sure if you’ve solved the problem yourself by now but for future readers:

I’ve found that the problem arises from the AlignType (PrimaryAxisLookAt) . A working solution would use the default AlignType (AllAxes) and update the AlignOrientation’s CFrame to look at the target position in a loop.

Since I completely forgot about this post and it has been a few months since I already fixed this, let me explain on how I fixed this myself.

First what I did was make a part that would slowly turn to the target, so it doesn’t make the character instantly turn at the target and fling the character.

The character will practically do the same, instant rotation, but instead of instantly rotating to the target, it would instead lock on to the part, so it doesn’t rotate instantly.

This was the best way I could explain this, since I am bad at explaining stuff and there is DEFINITELY a better way to make this work more, but for now, I’ll just stick with this since it doesn’t make any bugs and works completely fine right now.

Thanks to anyone for trying to help!