How to make Motor6D look at target

So my Ballista doesn’t look at the target but instead just looks away from it.

local function LookAt(part)
	if part then
		local targetPosition = PredictTargetPosition(part)
		local lookVector = (targetPosition - FirePoint.Position).Unit
		
		local maxYaw = math.rad(90)
		local maxPitch = math.rad(45)
		
		local yaw = math.clamp(math.atan2(lookVector.X, lookVector.Z), -maxYaw, maxYaw)
		local pitch = math.clamp(math.asin(-lookVector.Y), -maxPitch, maxPitch)

		local rotation = CFrame.Angles(pitch, yaw, 0)
		MoverPart.Main.C0 = MoverPart.Main.C0:Lerp(rotation, 0.05)
	else
		local rotation = CFrame.Angles(0, 0, 0)
		MoverPart.Main.C0 = MoverPart.Main.C0:Lerp(rotation, 0.05)
	end
end

Models are odd with it. Worst comes to worst, just add math.rad(180) to the CFrame coordinate. Several of my models end up upside down after using :pivotto when it makes no sense.

1 Like

I actually fixed it!

local function LookAt(part)
	if part then
		local targetPosition = PredictTargetPosition(part)
		local lookVector = (MoverPart.Position - targetPosition).Unit
		lookVector = MoverPart.CFrame:VectorToObjectSpace(lookVector)

		local rotation = CFrame.new(Vector3.new(), lookVector)

		MoverPart.Main.C0 = MoverPart.Main.C0:Lerp(rotation, 0.05)
	else
		local rotation = CFrame.new(Vector3.new())
		MoverPart.Main.C0 = MoverPart.Main.C0:Lerp(rotation, 0.05)
	end
end
2 Likes

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