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.